"use client";

import { useActionState, useEffect } from "react";
import Link from "next/link";
import { useFormStatus } from "react-dom";
import { Loader2 } from "lucide-react";
import { registerAction, type AuthState } from "@/app/actions/auth";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

const initial: AuthState = { status: "idle" };

function Submit({ label, pendingLabel }: { label: string; pendingLabel: string }) {
  const { pending } = useFormStatus();
  return (
    <button
      type="submit"
      disabled={pending}
      className="flex h-12 w-full items-center justify-center gap-2 bg-charcoal text-[12px] uppercase tracking-[0.18em] text-ivory transition-opacity hover:opacity-90 disabled:opacity-60"
    >
      {pending ? (
        <>
          <Loader2 className="h-4 w-4 animate-spin" />
          {pendingLabel}
        </>
      ) : (
        label
      )}
    </button>
  );
}

export function RegisterForm({
  locale,
  labels,
  loginHref,
}: {
  locale: string;
  labels: {
    title: string;
    description: string;
    name: string;
    email: string;
    phone: string;
    password: string;
    confirmPassword: string;
    register: string;
    creating: string;
    hint: string;
    mismatch: string;
    hasAccount: string;
    login: string;
    terms: string;
    termsLink: string;
    privacyLink: string;
  };
  loginHref: string;
}) {
  const [state, action] = useActionState(registerAction, initial);

  useEffect(() => {
    // Keep server validation messages visible; no toast needed.
  }, [state]);

  return (
    <div className="border border-line bg-cream p-6 shadow-panel md:p-8">
      <p className="eyebrow">DAR ZELLIGE</p>
      <h1 className="mt-4 font-serif text-4xl font-light tracking-tight">{labels.title}</h1>
      <p className="mt-3 text-[14px] leading-relaxed text-walnut">{labels.description}</p>

      {state.status === "error" && (
        <p className="mt-5 border border-terracotta/30 bg-terracotta/5 px-4 py-3 text-[14px] text-terracotta">
          {state.message}
        </p>
      )}

      <form action={action} className="mt-7 space-y-4">
        <input type="hidden" name="locale" value={locale} />
        <div>
          <Label htmlFor="name" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.name}
          </Label>
          <Input id="name" name="name" required autoComplete="name" className="h-11 bg-cream" />
        </div>
        <div>
          <Label htmlFor="email" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.email}
          </Label>
          <Input id="email" name="email" type="email" required autoComplete="email" className="h-11 bg-cream" />
        </div>
        <div>
          <Label htmlFor="phone" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.phone}
          </Label>
          <Input id="phone" name="phone" type="tel" autoComplete="tel" className="h-11 bg-cream" />
        </div>
        <div>
          <Label htmlFor="password" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.password}
          </Label>
          <Input
            id="password"
            name="password"
            type="password"
            required
            autoComplete="new-password"
            className="h-11 bg-cream"
          />
          <p className="mt-1.5 text-[12px] leading-relaxed text-walnut">{labels.hint}</p>
        </div>
        <div>
          <Label htmlFor="confirmPassword" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.confirmPassword}
          </Label>
          <Input
            id="confirmPassword"
            name="confirmPassword"
            type="password"
            required
            autoComplete="new-password"
            className="h-11 bg-cream"
          />
        </div>

        <label className="flex items-start gap-2.5 pt-1 text-[13px] leading-relaxed text-walnut">
          <input type="checkbox" required className="mt-0.5 h-3.5 w-3.5 accent-[#24221D]" />
          <span>
            {labels.terms}{" "}
            <Link href={`/${locale}/terms`} className="text-charcoal underline underline-offset-4">
              {labels.termsLink}
            </Link>{" "}
            &amp;{" "}
            <Link href={`/${locale}/privacy`} className="text-charcoal underline underline-offset-4">
              {labels.privacyLink}
            </Link>
          </span>
        </label>

        <Submit label={labels.register} pendingLabel={labels.creating} />
      </form>

      <p className="mt-7 text-[14px] text-walnut">
        {labels.hasAccount}{" "}
        <Link href={loginHref} className="text-charcoal underline underline-offset-4">
          {labels.login}
        </Link>
      </p>
    </div>
  );
}
