"use client";

import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import AuthEntryModal from "./AuthEntryModal";
import AuthRecoveryModals from "./AuthRecoveryModals";
import LearnServeConfirmationModals from "./LearnServeConfirmationModals";
import VolunteerMandateDetails from "@/features/auth/components/VolunteerMandateDetails";
import { type LearnServeRegistrationDetails } from "./ConfirmRegistrationModal";
import type { MandatePrefillUser } from "@/features/auth/components/volunteerMandateParts";
import type { OtpType } from "@/features/auth/services/authApi";

interface LearnServeRegistrationModalsProps {
  /** The main dialog: sign-in, sign-up, or the consultation booking. */
  open: boolean;
  setOpen: (open: boolean) => void;
  modalState: 1 | 2 | 3;
  setModalState: (state: 1 | 2 | 3) => void;
  modalType: "auth" | "consultation";
  setModalType: (type: "auth" | "consultation") => void;

  /** The two confirmations, owned by the page because it decides when to ask. */
  showConfirmRegistrationModal: boolean;
  setShowConfirmRegistrationModal: (open: boolean) => void;
  showUnregisterModal: boolean;
  setShowUnregisterModal: (open: boolean) => void;

  /** The opportunity being registered for; set by the time a flow can open. */
  id: string;
  /** The record, for the title and cover shown in the confirmations. */
  opportunityData?: {
    id?: string | number;
    opportunity_images?: Array<{ image?: string }> | null;
  } | null;
  opportunityDetails: LearnServeRegistrationDetails;
  /** The consultation booking dialog, whose trigger the page owns. */
  showConsultationModal: boolean;
  setShowConsultationModal: (open: boolean) => void;
  refetch: () => void;
}

/**
 * Everything between "Register" and a registration: sign in, sign up, email
 * verification, forgot / reset password, and the mandate-details step — plus
 * the two confirmations.
 *
 * The secondary dialogs' state lives here rather than on the page, because
 * every hop between them (login → sign-up, login → forgot password,
 * verification → reset → back to login) is internal to the flow. The page only
 * decides *whether* the flow is open. This mirrors `RegistrationFlowModals`,
 * which does the same job for the volunteering page.
 */
export default function LearnServeRegistrationModals({
  open,
  setOpen,
  modalState,
  setModalState,
  modalType,
  setModalType,
  showConfirmRegistrationModal,
  setShowConfirmRegistrationModal,
  showUnregisterModal,
  setShowUnregisterModal,
  id,
  opportunityData,
  opportunityDetails,
  showConsultationModal,
  setShowConsultationModal,
  refetch,
}: LearnServeRegistrationModalsProps) {
  const { t } = useTranslation();

  const [isSubmitting] = useState(false);
  const [showEmailVerification, setShowEmailVerification] = useState(false);
  const [verificationEmail, setVerificationEmail] = useState("");
  const [otpType, setOtpType] = useState<OtpType>("register");
  const [showForgotPassword, setShowForgotPassword] = useState(false);
  const [showResetPassword, setShowResetPassword] = useState(false);
  const [resetPasswordData, setResetPasswordData] = useState<{
    email: string;
    token: string;
  } | null>(null);
  const [showVolunteerMandateDetails, setShowVolunteerMandateDetails] =
    useState(false);
  const [mandateDetailsData, setMandateDetailsData] =
    useState<MandatePrefillUser | null>(null);

  const handleShowEmailVerification = (email: string, type: OtpType) => {
    setVerificationEmail(email);
    setOtpType(type);
    setShowEmailVerification(true);
  };

  const handleShowVolunteerMandateDetails = (userData: MandatePrefillUser) => {
    setMandateDetailsData(userData);
    setShowVolunteerMandateDetails(true);
  };

  const handleShowLogin = () => {
    setModalState(1);
    setModalType("auth");
    setOpen(true);
  };

  const handleShowForgotPassword = () => {
    setShowForgotPassword(true);
    setOpen(false);
  };

  const handleShowResetPassword = (email: string, token: string) => {
    setResetPasswordData({ email, token });
    setShowResetPassword(true);
    setShowEmailVerification(false);
  };

  return (
    <>
      <AuthEntryModal
        open={open}
        setOpen={setOpen}
        modalState={modalState}
        setModalState={setModalState}
        modalType={modalType}
        isSubmitting={isSubmitting}
        onShowEmailVerification={handleShowEmailVerification}
        onShowForgotPassword={handleShowForgotPassword}
        onShowVolunteerMandateDetails={handleShowVolunteerMandateDetails}
      />

      <AuthRecoveryModals
        showEmailVerification={showEmailVerification}
        setShowEmailVerification={setShowEmailVerification}
        verificationEmail={verificationEmail}
        otpType={otpType}
        showForgotPassword={showForgotPassword}
        setShowForgotPassword={setShowForgotPassword}
        showResetPassword={showResetPassword}
        setShowResetPassword={setShowResetPassword}
        resetPasswordData={resetPasswordData}
        onShowLogin={handleShowLogin}
        onShowResetPassword={handleShowResetPassword}
        onShowEmailVerification={handleShowEmailVerification}
      />
    <Modal
      open={showVolunteerMandateDetails}
      onClose={() => setShowVolunteerMandateDetails(false)}
      title={t("COMMON.COMPLETEYOURDETAILS")}
      size="md"
    >
      <VolunteerMandateDetails
        userData={mandateDetailsData}
        onClose={() => setShowVolunteerMandateDetails(false)}
        isModal
      />
    </Modal>

      <LearnServeConfirmationModals
        modalType={modalType}
        showConsultationModal={showConsultationModal}
        setShowConsultationModal={setShowConsultationModal}
        showConfirmRegistrationModal={showConfirmRegistrationModal}
        setShowConfirmRegistrationModal={setShowConfirmRegistrationModal}
        showUnregisterModal={showUnregisterModal}
        setShowUnregisterModal={setShowUnregisterModal}
        id={id}
        opportunityData={opportunityData}
        opportunityDetails={opportunityDetails}
        refetch={refetch}
      />

    {/* <div className="relative w-full">
      <img
        className="w-full h-[320px] object-cover"
        src={opportunityData?.opportunity_images?.[0]?.image}
        alt=""
      />
    </div> */}
    </>
  );
}
