"use client";

import Link from "next/link";
import Image from "next/image";
import { Moon, Sun } from "lucide-react";
import AmbientScene from "./AmbientScene";
import Particles from "./Particles";
import RevealText from "./RevealText";
import Reveal from "./Reveal";
import { useLandingTheme } from "./useLandingTheme";

/**
 * Shared shell for the auth flows (login, register, add-subdomain) — the same
 * ambient dark scene, particle field and card language as the landing page's
 * hero and its /contact form, scaled down to one centred card instead of a
 * marketing nav. Each page owns its own form and just supplies the copy.
 */
export default function AuthShell({
  eyebrow,
  title,
  description,
  children,
  footer,
  cardClassName,
}: {
  eyebrow: string;
  title: string;
  description?: string;
  children: React.ReactNode;
  footer?: React.ReactNode;
  cardClassName?: string;
}) {
  const { theme, toggleTheme } = useLandingTheme();

  return (
    <div
      className={`immersive relative flex min-h-screen flex-col${
        theme === "light" ? " immersive--light" : ""
      }`}
    >
      <noscript>
        <style>{`.immersive [data-reveal],.immersive [data-reveal-text]{opacity:1!important}`}</style>
      </noscript>

      <AmbientScene theme={theme} />
      <div className="pointer-events-none fixed inset-0 z-[1]" aria-hidden>
        <Particles />
      </div>

      <header className="relative z-10 flex items-center justify-between px-6 py-6 lg:px-12 lg:py-8">
        <Link href="/" aria-label="Oxyfinz home" className="flex shrink-0 items-center">
          <Image
            src="/assets/logooxy.png"
            alt="Oxyfinz"
            width={160}
            height={52}
            className="h-7 w-auto object-contain"
            priority
          />
        </Link>
        <button
          type="button"
          onClick={toggleTheme}
          aria-label={theme === "dark" ? "Switch to light theme" : "Switch to dark theme"}
          title={theme === "dark" ? "Switch to light theme" : "Switch to dark theme"}
          className="inline-flex size-9 items-center justify-center rounded-md border border-white/12 text-[hsl(var(--foreground))]/80 transition-colors hover:border-white/30 hover:text-[hsl(var(--foreground))]"
        >
          {theme === "dark" ? (
            <Sun className="size-4" aria-hidden />
          ) : (
            <Moon className="size-4" aria-hidden />
          )}
        </button>
      </header>

      <main className="relative z-10 flex flex-1 items-center justify-center px-6 pb-16 pt-4 lg:px-12">
        <div className="w-full max-w-md">
          <div className="mb-8 text-center">
            <span className="immersive-label">{eyebrow}</span>
            <RevealText as="h1" immediate className="immersive-heading mt-3">
              {title}
            </RevealText>
            {description ? (
              <p className="mt-3 text-sm leading-relaxed text-[hsl(var(--muted-foreground))]">
                {description}
              </p>
            ) : null}
          </div>

          <Reveal delay={0.1} className={`immersive-card p-7 lg:p-8 ${cardClassName ?? ""}`}>
            {children}
          </Reveal>

          {footer ? <div className="mt-6 text-center">{footer}</div> : null}
        </div>
      </main>
    </div>
  );
}
