"use client";

import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import EmailVerificationForm from "@/features/auth/components/EmailVerificationForm";
import ForgotPasswordForm from "@/features/auth/components/ForgotPasswordForm";
import ResetPasswordForm from "@/features/auth/components/ResetPasswordForm";
import type { OtpType } from "@/features/auth/services/authApi";

interface AuthRecoveryModalsProps {
  showEmailVerification: boolean;
  setShowEmailVerification: (open: boolean) => void;
  verificationEmail: string;
  otpType: OtpType;

  showForgotPassword: boolean;
  setShowForgotPassword: (open: boolean) => void;

  showResetPassword: boolean;
  setShowResetPassword: (open: boolean) => void;
  resetPasswordData: { email: string; token: string } | null;

  /** Hops between the dialogs, all of which end back at sign-in. */
  onShowLogin: () => void;
  onShowResetPassword: (email: string, token: string) => void;
  onShowEmailVerification: (email: string, type: OtpType) => void;
}

/**
 * The three dialogs a sign-in can detour through: email verification, forgot
 * password and reset password.
 *
 * Separate from the main sign-in / sign-up dialog because each of these is a
 * *detour* — every one of them ends by returning to sign-in, which is why they
 * share one set of callbacks.
 */
export default function AuthRecoveryModals({
  showEmailVerification,
  setShowEmailVerification,
  verificationEmail,
  otpType,
  showForgotPassword,
  setShowForgotPassword,
  showResetPassword,
  setShowResetPassword,
  resetPasswordData,
  onShowLogin,
  onShowResetPassword,
  onShowEmailVerification,
}: AuthRecoveryModalsProps) {
  const { t } = useTranslation();

  return (
    <>
    <Modal
      open={showEmailVerification}
      onClose={() => setShowEmailVerification(false)}
      title={t("COMMON.EMAIL_VERIFICATION")}
      size="md"
    >
      <EmailVerificationForm
        email={verificationEmail}
        otp_type={otpType}
        onClose={() => setShowEmailVerification(false)}
        onShowLogin={onShowLogin}
        onShowResetPassword={onShowResetPassword}
        isModal
      />
    </Modal>

    <Modal
      open={showForgotPassword}
      onClose={() => setShowForgotPassword(false)}
      title={t("COMMON.FORGOT_PASSWORD")}
      size="sm"
    >
      <ForgotPasswordForm
        onShowEmailVerification={onShowEmailVerification}
        onClose={() => setShowForgotPassword(false)}
        isModal
      />
    </Modal>

    <Modal
      open={showResetPassword}
      onClose={() => setShowResetPassword(false)}
      title={t("COMMON.RESET_PASSWORD")}
      size="md"
    >
      <ResetPasswordForm
        email={resetPasswordData?.email || ""}
        token={resetPasswordData?.token || ""}
        onClose={() => setShowResetPassword(false)}
        onShowLogin={onShowLogin}
        isModal
      />
    </Modal>

    </>
  );
}
