"use client";

import { useActionState, useEffect } from "react";
import { useFormStatus } from "react-dom";
import { Loader2 } from "lucide-react";
import { toast } from "sonner";
import { updateProfileAction, type AccountState } from "@/app/actions/account";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

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

function Submit({ label }: { label: string }) {
  const { pending } = useFormStatus();
  return (
    <button
      type="submit"
      disabled={pending}
      className="flex h-11 items-center justify-center gap-2 bg-charcoal px-6 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}
      {label}
    </button>
  );
}

export function ProfileForm({
  locale,
  user,
  labels,
}: {
  locale: string;
  user: { name: string; email: string; phone: string };
  labels: { title: string; desc: string; name: string; email: string; phone: string; save: string; updated: string };
}) {
  const [state, action] = useActionState(updateProfileAction, initial);

  useEffect(() => {
    if (state.status === "success") toast.success(labels.updated);
  }, [state, labels.updated]);

  const errorFor = (field: string) => state.fieldErrors?.[field]?.[0];

  return (
    <div className="border border-line bg-cream p-6 shadow-panel md:p-8">
      <div className="border-b border-line pb-4">
        <h2 className="font-serif text-3xl font-light">{labels.title}</h2>
        <p className="mt-1.5 text-[14px] leading-relaxed text-walnut">{labels.desc}</p>
      </div>

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

      <form action={action} className="mt-6 max-w-lg 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" defaultValue={user.name} required className="h-11 bg-cream" />
          {errorFor("name") && <p className="mt-1 text-[12px] text-terracotta">{errorFor("name")}</p>}
        </div>
        <div>
          <Label htmlFor="email" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.email}
          </Label>
          <Input id="email" value={user.email} disabled className="h-11 bg-parchment text-walnut" />
        </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" defaultValue={user.phone} className="h-11 bg-cream" />
        </div>
        <Submit label={labels.save} />
      </form>
    </div>
  );
}
