import React from "react";
import { useSpeakerAwayLogManager } from "../../../../../services/Services";
import { SettingsDeleteView } from "../../../../../components/business/views";
import { CalendarDaysIcon, UserIcon, ClockIcon, InformationCircleIcon } from "@heroicons/react/24/outline";
import { formatDateForDisplay } from "../../../../../services/Helpers/DateFormatter";

function SettingSpeakerAwayLogDeletePage() {
  const speakerAwayLogManager = useSpeakerAwayLogManager();

  // Helper function to create a meaningful name from speaker away log data
  const createAwayLogName = (awayLogData) => {
    if (!awayLogData) return "Unknown";

    const speakerName = awayLogData.speakerName || `Speaker #${awayLogData.speakerId}` || "Unknown Speaker";
    const startDate = formatDateForDisplay(awayLogData.startDate) || "Unknown Date";

    // Create a truncated name: "John Smith - 2024-01-15" (max ~40 chars)
    const fullName = `${speakerName} - ${startDate}`;
    return fullName.length > 40 ? `${fullName.substring(0, 37)}...` : fullName;
  };

  // Custom entity transformer to use "delete" confirmation with meaningful display
  const transformEntityData = (entityData) => {
    if (!entityData) return entityData;

    const displayName = createAwayLogName(entityData);

    return {
      ...entityData,
      // Set name to "delete" for confirmation requirement
      name: "delete",
      // Add detailed description to provide meaningful context
      description: `${displayName}\n\nThis away log marks ${entityData.speakerName || 'the speaker'} as unavailable from ${formatDateForDisplay(entityData.startDate) || 'unknown date'}${entityData.untilFurtherNotice === 1 ? ' until further notice' : ` until ${formatDateForDisplay(entityData.untilDate) || 'unknown date'}`}.`
    };
  };

  // Create a wrapper around the speaker away log manager to transform data
  const speakerAwayLogManagerWrapper = {
    ...speakerAwayLogManager,
    getSpeakerAwayLogDetail: async (id, onUnauthorized) => {
      const data = await speakerAwayLogManager.getSpeakerAwayLogDetail(id, onUnauthorized);
      return transformEntityData(data);
    },
    deleteSpeakerAwayLog: async (id, onUnauthorized) => {
      return await speakerAwayLogManager.deleteSpeakerAwayLog(id, onUnauthorized);
    }
  };

  // Define impact warnings
  const impactWarnings = [
    {
      icon: UserIcon,
      text: "The speaker will no longer be marked as unavailable for this period"
    },
    {
      icon: ClockIcon,
      text: "Historical records referencing this away log will still exist but may show as 'Deleted'"
    },
    {
      icon: InformationCircleIcon,
      text: "You will not be able to recover this away log once deleted"
    }
  ];

  // Define alternative text
  const alternativeText = "Consider editing the away log to update the end date instead of deleting it to preserve historical data.";

  return (
    <SettingsDeleteView
      entityType="speakerAwayLog"
      entityTypePlural="Speaker Away Logs"
      entityIcon={CalendarDaysIcon}
      entityManager={speakerAwayLogManagerWrapper}
      basePath="/admin/settings/speaker-away-log"
      listPath="/admin/settings/speaker-away-logs"
      title="Delete Speaker Away Log"
      subtitle="Permanent deletion confirmation"
      impactWarnings={impactWarnings}
      alternativeText={alternativeText}
      requiresConfirmation={true}
    />
  );
}

export default SettingSpeakerAwayLogDeletePage;