"use client";

import { useRouter } from "next/navigation";
import { createPortal } from "react-dom";
import SigninModal from "./Signin";
import SignupModal from "./Signup";
import OtpModal from "./OTPVerification";
import { assetPath } from "@/utils/assets";

const slides = [
  {
    src: assetPath("/assets/img/login/1.jpg"),
    title: "Sign Up Now and",
    subtitle: "Take Control of a Smarter, More Flexible Travel Experience",
  },
  {
    src: assetPath("/assets/img/login/2.jpg"),
    title: "Sign Up Now",
    subtitle: "Unlock Exclusive Travel Benefits",
  },
  {
    src: assetPath("/assets/img/login/3.jpg"),
    title: "Sign Up Today & Travel with",
    subtitle: "1M+ Happy Travelers",
  },
];

interface AuthModalProps {
  view: "signup" | "signin" | "otp";
  setView: (view: "signup" | "signin" | "otp") => void;
  onClose: () => void;
  /** If set, redirect to this URL after successful signin/signup (e.g. back to hotel details). */
  redirectAfterAuth?: string;
}

export default function AuthModal({ view, setView, onClose, redirectAfterAuth }: AuthModalProps) {
  const router = useRouter();
  const canUseDOM = typeof window !== "undefined" && typeof document !== "undefined";

  const handleAuthSuccess = () => {
    if (redirectAfterAuth) {
      router.push(redirectAfterAuth);
    }
    onClose();
  };

  const handleBackdropClick = (e: React.MouseEvent<HTMLDivElement>) => {
    if (e.target === e.currentTarget && view !== "otp") {
      onClose();
    }
  };

  if (!canUseDOM) return null;

  return createPortal(
    <div
      className="auth-modal"
      onClick={handleBackdropClick}
      /**
       * React propagates events through the *React* tree, not the DOM tree, so
       * a portal's events still reach whatever component rendered it. The
       * signin/signup/OTP panes each have their own `<form>`, and when this
       * modal is opened from inside another form — the flight passenger
       * details form does exactly that — their submit bubbled straight into
       * it, running that form's validation and firing its error toast.
       *
       * Contained here rather than at each call site: a modal's submit is
       * never meant for an ancestor, whoever happens to have rendered it.
       */
      onSubmit={(event) => event.stopPropagation()}
    >
      {view === "signup" && (
        <SignupModal
          goSignin={() => setView("signin")}
          onClose={onClose}
          onAuthSuccess={handleAuthSuccess}
          slides={slides}
        />
      )}

      {view === "signin" && (
        <SigninModal
          goSignup={() => setView("signup")}
          goOtp={() => setView("otp")}
          slides={slides}
          onClose={onClose}
          onAuthSuccess={handleAuthSuccess}
        />
      )}

      {view === "otp" && (
        <OtpModal
          goSignup={() => setView("signup")}
          goSignin={() => setView("signin")}
          onClose={onClose}
          onAuthSuccess={handleAuthSuccess}
        />
      )}
    </div>,
    document.body
  );
}
