"use client";

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 type { MandatePrefillUser } from "@/features/auth/components/volunteerMandateParts";
import type { OtpType } from "@/features/auth/services/authApi";

interface AuthEntryModalProps {
  open: boolean;
  setOpen: (open: boolean) => void;
  /** 1 = sign in, 2 = sign up, 3 = the role picker. */
  modalState: 1 | 2 | 3;
  setModalState: (state: 1 | 2 | 3) => void;
  modalType: "auth" | "consultation";
  isSubmitting: boolean;

  /** Detours out of sign-in, all of which come back to it. */
  onShowEmailVerification: (email: string, type: OtpType) => void;
  onShowForgotPassword: () => void;
  onShowVolunteerMandateDetails: (userData: MandatePrefillUser) => void;
}

/**
 * The one dialog a "Register" click opens: sign in, or sign up.
 *
 * Which of the two it shows is `modalState`, and the switch between them is
 * internal — a visitor who starts at sign-in and realises they have no account
 * should not have the dialog close and reopen underneath them.
 */
export default function AuthEntryModal({
  open,
  setOpen,
  modalState,
  setModalState,
  modalType,
  isSubmitting,
  onShowEmailVerification,
  onShowForgotPassword,
  onShowVolunteerMandateDetails,
}: AuthEntryModalProps) {
  const { t } = useTranslation();

  return (
    <Modal
      open={modalType === "auth" && open}
      onClose={() => setOpen(false)}
      disableFilterModalClass
      title={
        modalState === 1
          ? t("COMMON.JOINUS")
          : modalState === 2
            ? t("COMMON.SIGN.UP")
            : t("COMMON.ROLE")
      }
      size={modalState === 2 ? "md" : "sm"}
      footer={
        modalState === 3 ? (
          <div className="flex justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xss:!w-full"
              onClick={() => {
                const form = document.querySelector("form") as HTMLFormElement;
                if (form) form.requestSubmit();
              }}
              disabled={isSubmitting}
            >
              {t("COMMON.CONFIRM")}
            </Button>
          </div>
        ) : null
      }
    >
      {modalState === 1 ? (
        <LoginForm
          isModal
          onClose={() => setOpen(false)}
          onShowEmailVerification={(email) => {
            setOpen(false);
            onShowEmailVerification(email, "register");
          }}
          onShowRegistration={() => {
            setModalState(2);
            setOpen(true);
          }}
          onShowVolunteerMandateDetails={onShowVolunteerMandateDetails}
          onShowForgotPassword={onShowForgotPassword}
        />
      ) : modalState === 2 ? (
        <RegisterVolunteerModalForm
          onClose={() => setOpen(false)}
          onShowEmailVerification={onShowEmailVerification}
          onShowVolunteerMandateDetails={onShowVolunteerMandateDetails}
        />
      ) : null}
    </Modal>
  );
}
