"use client";

import { useActionState, useEffect, useRef } from "react";
import { useFormStatus } from "react-dom";
import { Loader2 } from "lucide-react";
import { toast } from "sonner";
import { changePasswordAction, 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 SecurityForm({
  locale,
  labels,
}: {
  locale: string;
  labels: {
    title: string;
    desc: string;
    current: string;
    next: string;
    confirm: string;
    update: string;
    hint: string;
    updated: string;
  };
}) {
  const [state, action] = useActionState(changePasswordAction, initial);
  const formRef = useRef<HTMLFormElement>(null);

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

  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 ref={formRef} action={action} className="mt-6 max-w-lg space-y-4">
        <input type="hidden" name="locale" value={locale} />
        <div>
          <Label htmlFor="currentPassword" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.current}
          </Label>
          <Input id="currentPassword" name="currentPassword" type="password" required autoComplete="current-password" className="h-11 bg-cream" />
        </div>
        <div>
          <Label htmlFor="newPassword" className="mb-2 block text-[12px] uppercase tracking-[0.1em] text-walnut">
            {labels.next}
          </Label>
          <Input id="newPassword" name="newPassword" 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.confirm}
          </Label>
          <Input id="confirmPassword" name="confirmPassword" type="password" required autoComplete="new-password" className="h-11 bg-cream" />
        </div>
        <Submit label={labels.update} />
      </form>
    </div>
  );
}
