"use client";

import { useActionState, useEffect } from "react";
import { useFormStatus } from "react-dom";
import { CheckCircle2, Loader2 } from "lucide-react";
import { sendContactAction, type ContactState } from "@/app/actions/contact";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";

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

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

export function ContactForm({
  labels,
  locale,
}: {
  locale: string;
  labels: {
    name: string;
    email: string;
    subject: string;
    message: string;
    send: string;
    sending: string;
    sent: string;
    error: string;
  };
}) {
  const [state, action] = useActionState(sendContactAction, initial);

  useEffect(() => {
    if (state.status === "success") {
      const form = document.getElementById("contact-form") as HTMLFormElement | null;
      form?.reset();
    }
  }, [state]);

  if (state.status === "success") {
    return (
      <div className="flex min-h-[320px] flex-col items-center justify-center border border-olive/25 bg-olive/5 p-8 text-center">
        <CheckCircle2 className="h-7 w-7 text-olive" strokeWidth={1.2} />
        <p className="mt-4 font-serif text-2xl font-light">{labels.sent}</p>
      </div>
    );
  }

  return (
    <form id="contact-form" action={action} className="space-y-4">
      <input type="hidden" name="locale" value={locale} />
      {state.status === "error" && state.message && (
        <p className="border border-terracotta/25 bg-terracotta/5 px-4 py-3 text-[13px] text-terracotta">
          {state.message}
        </p>
      )}
      <div className="grid gap-4 sm:grid-cols-2">
        <div>
          <Label htmlFor="contact-name" className="mb-2 block text-[12px] uppercase tracking-[0.14em] text-charcoal/50">
            {labels.name}
          </Label>
          <Input id="contact-name" name="name" required className="h-11 bg-transparent" autoComplete="name" />
        </div>
        <div>
          <Label htmlFor="contact-email" className="mb-2 block text-[12px] uppercase tracking-[0.14em] text-charcoal/50">
            {labels.email}
          </Label>
          <Input id="contact-email" name="email" type="email" required className="h-11 bg-transparent" autoComplete="email" />
        </div>
      </div>
      <div>
        <Label htmlFor="contact-subject" className="mb-2 block text-[12px] uppercase tracking-[0.14em] text-charcoal/50">
          {labels.subject}
        </Label>
        <Input id="contact-subject" name="subject" required className="h-11 bg-transparent" />
      </div>
      <div>
        <Label htmlFor="contact-message" className="mb-2 block text-[12px] uppercase tracking-[0.14em] text-charcoal/50">
          {labels.message}
        </Label>
        <Textarea id="contact-message" name="message" rows={6} required className="bg-transparent" />
      </div>
      <Submit label={labels.send} pendingLabel={labels.sending} />
    </form>
  );
}