// File Path: monorepo/web/frontend/src/pages/Admin/Event/Virtual/Add/Step4Page.jsx

import React from "react";
import { useVirtualEventWizardStorage } from "../../../../../services/Services";
import { EventStep5Component } from "../../../../../components/business/events";
import {
  ComputerDesktopIcon,
  CalendarIcon,
  UserGroupIcon,
} from "@heroicons/react/24/outline";
import { EVENT_TYPE_VIRTUAL } from "../../../../../constants/Event";
import { FormSection, InfoField } from "../../../../../components/UIX";
import { formatDateForDisplay } from "../../../../../services/Helpers/DateFormatter";

// Virtual event type configuration
const virtualConfig = {
  eventType: EVENT_TYPE_VIRTUAL,
  labels: {
    wizardTitle: "Add New Virtual Event",
    eventTypeName: "Virtual Event",
    backToList: "Back to Virtual Events",
  },
  icons: {
    wizard: ComputerDesktopIcon,
  },
  routes: {
    list: "/admin/events/virtuals",
    step1: "/admin/events/virtuals/add/step-1",
    step2: "/admin/events/virtuals/add/step-2",
    step3: "/admin/events/virtuals/add/step-3",
    step4: "/admin/events/virtuals/add/step-4",
  },
};

// Wrapper for Virtual storage to match the generic interface
const createVirtualStorageWrapper = (virtualEventWizardStorage) => ({
  getWizardData: () => virtualEventWizardStorage.getVirtualEventWizard(),
  saveWizardData: (data) =>
    virtualEventWizardStorage.saveVirtualEventWizard(data),
  clearWizardData: () => virtualEventWizardStorage.clearVirtualEventWizard(),
});

// Virtual-specific review sections
const virtualCustomReviewSections = (wizardData, onUnauthorized) => {
  // Guard against null wizardData (can happen after successful submission during navigation delay)
  if (!wizardData) return null;

  return (
    <>
      {/* Virtual Meeting Information Section */}
      <FormSection
        title="Virtual Meeting Details"
        description="Virtual meeting platform and access"
        className="mb-6"
      >
        <div className="grid grid-cols-1 gap-4 sm:gap-6">
          {wizardData.meetingURL && (
            <InfoField label="Meeting URL" value={wizardData.meetingURL} />
          )}
          {wizardData.meetingPlatform && (
            <InfoField
              label="Meeting Platform"
              value={wizardData.meetingPlatform}
            />
          )}
        </div>
      </FormSection>

      {/* Schedule Details */}
      <FormSection className="mb-6">
        <InfoField
          label="Event Schedule"
          value={
            wizardData.startDate && wizardData.endDate
              ? `${formatDateForDisplay(wizardData.startDate)} - ${formatDateForDisplay(wizardData.endDate)}`
              : wizardData.date && wizardData.startTime && wizardData.endTime
                ? `${wizardData.date} from ${wizardData.startTime} to ${wizardData.endTime}`
                : "Not specified"
          }
          icon={CalendarIcon}
        />
        {wizardData.attendeeCapacity && (
          <InfoField
            label="Expected Attendees"
            value={`${wizardData.attendeeCapacity} people`}
            icon={UserGroupIcon}
          />
        )}
      </FormSection>
    </>
  );
};

// Virtual-specific payload processing
const virtualCustomPayloadProcessing = (payload, wizardData) => ({
  ...payload,
  // Virtual-specific fields
  location: "Virtual Meeting", // Default location for virtual events
  meetingURL: wizardData.meetingURL || "",
  meetingPlatform: wizardData.meetingPlatform || "",
  partners: wizardData.partners || "",
  listing_url: wizardData.listingUrl || "",
  dayOfWeek: wizardData.dayOfWeek || "Monday",
  // isSHSM is now a boolean - convert legacy int values if needed
  isSHSM:
    wizardData.isSHSM === true || wizardData.isSHSM === 1
      ? true
      : wizardData.isSHSM === false || wizardData.isSHSM === 2
        ? false
        : null,
  facilitatorAM: wizardData.facilitatorAM || false,
  facilitatorPM: wizardData.facilitatorPM || false,
  eventType: EVENT_TYPE_VIRTUAL, // Always set to Virtual type
  attendeeCapacity: parseInt(wizardData.attendeeCapacity) || 0,
});

function AdminEventVirtualAddStep5Page() {
  const virtualEventWizardStorage = useVirtualEventWizardStorage();
  const wrappedStorage = createVirtualStorageWrapper(virtualEventWizardStorage);

  return (
    <EventStep5Component
      wizardStorage={wrappedStorage}
      eventTypeConfig={virtualConfig}
      customReviewSections={virtualCustomReviewSections}
      customPayloadProcessing={virtualCustomPayloadProcessing}
    />
  );
}

export default AdminEventVirtualAddStep5Page;
