"use client";

import { formatHoursMinutes } from "@/features/shared/clientReviewPolicy";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/Button";
import { Modal } from "@/components/ui/Modal";
import LoginForm from "@/features/auth/components/LoginForm";
import RegisterVolunteerModalForm from "@/features/auth/components/RegisterVolunteerModalForm";
import ConfirmVolunteerRegistrationModal from "../ConfirmVolunteerRegistrationModal";
import UnregisterConfirmationModal from "../UnregisterConfirmationModal";
import VolunteerRegisterRoleModal, {
  VOLUNTEER_ROLE_REGISTRATION_FORM_ID,
  type OpportunityRegistrationDetails,
} from "../VolunteerRegisterRoleModal";
import { STEP_TITLE_KEYS, type RegistrationStep } from "./registrationStep";
import type { MandatePrefillUser } from "@/features/auth/components/volunteerMandateParts";
import type { OtpType } from "@/features/auth/services/authApi";

interface RegistrationStepModalProps {
  step: RegistrationStep | null;
  onStepChange: (step: RegistrationStep | null) => void;
  close: () => void;
  opportunityId: string;
  organizationId?: string;
  opportunityDetails: OpportunityRegistrationDetails;
  refetch: () => void;
  isSubmittingRoles: boolean;
  setIsSubmittingRoles: (submitting: boolean) => void;
  showEmailVerification: (email: string, otpType: OtpType) => void;
  showForgot: () => void;
  setMandateUser: (user: MandatePrefillUser) => void;
}

/**
 * The one dialog the registration flow keeps open, showing whichever step it is
 * on.
 *
 * One `Modal` rather than five, because the steps are hops within a single
 * journey — sign in, sign up, pick a role, confirm — and closing and reopening a
 * dialog underneath the user between them is exactly what the named-step model
 * was introduced to stop.
 *
 * The role step's submit button lives in the footer and reaches its form by
 * `form={VOLUNTEER_ROLE_REGISTRATION_FORM_ID}`, since the form itself is
 * rendered in the body by a child component.
 */
export default function RegistrationStepModal({
  step,
  onStepChange,
  close,
  opportunityId,
  organizationId,
  opportunityDetails,
  refetch,
  isSubmittingRoles,
  setIsSubmittingRoles,
  showEmailVerification,
  showForgot,
  setMandateUser,
}: RegistrationStepModalProps) {
  const { t } = useTranslation();

  return (
      <Modal
        open={step !== null}
        onClose={close}
        title={step ? t(STEP_TITLE_KEYS[step]) : ""}
        size={step === "signup" ? "md" : "sm"}
        disableFilterModalClass
        footer={
          step === "roles" ? (
            <div className="flex justify-center w-full gap-5">
              <Button
                variant="primary"
                size="medium"
                type="submit"
                form={VOLUNTEER_ROLE_REGISTRATION_FORM_ID}
                className="xss:!w-full"
                disabled={isSubmittingRoles}
              >
                {t("COMMON.CONFIRM")}
              </Button>
            </div>
          ) : null
        }
      >
        {(step === "confirm" || step === "roles") && opportunityDetails.daily_hours != null && <p className="mb-5 rounded-xl bg-gray-50 p-3 text-center text-sm leading-relaxed">{t("COMMON.SCHEDULED_HOURS_INFORMATION", { hours: formatHoursMinutes(opportunityDetails.daily_hours) })}</p>}
        {step === "login" ? (
          <LoginForm
            isModal
            onClose={close}
            onShowEmailVerification={(email) => {
              close();
              showEmailVerification(email, "register");
            }}
            onShowRegistration={() => onStepChange("signup")}
            onShowVolunteerMandateDetails={setMandateUser}
            onShowForgotPassword={showForgot}
          />
        ) : step === "signup" ? (
          <RegisterVolunteerModalForm
            onClose={close}
            onShowEmailVerification={showEmailVerification}
            onShowVolunteerMandateDetails={setMandateUser}
          />
        ) : step === "confirm" ? (
          <ConfirmVolunteerRegistrationModal
            opportunityId={opportunityId}
            onClose={close}
            refetch={refetch}
            organizationId={organizationId}
            opportunityDetails={opportunityDetails}
          />
        ) : step === "unregister" ? (
          <UnregisterConfirmationModal
            opportunityId={opportunityId}
            onClose={close}
            refetch={refetch}
          />
        ) : step === "roles" ? (
          <VolunteerRegisterRoleModal
            opportunityId={opportunityId}
            organizationId={organizationId}
            onLoadingChange={setIsSubmittingRoles}
            onClose={close}
            opportunityDetails={opportunityDetails}
          />
        ) : null}
      </Modal>
  );
}
