"use client";

import { useActionState } from "react";
import { AlertCircle, CheckCircle2, Lock, Loader2 } from "lucide-react";
import { changePassword, type ChangePasswordState } from "./actions";
import { Button } from "@/components/Button";

const initial: ChangePasswordState = { ok: false };

export function ChangePasswordForm() {
  const [state, action, pending] = useActionState(changePassword, initial);
  return (
    <form action={action} className="space-y-5">
      {state.message && (
        <div
          role="alert"
          className={
            state.ok
              ? "flex items-start gap-2 rounded-md border border-emerald-200 bg-emerald-50 p-3 text-sm text-emerald-900"
              : "flex items-start gap-2 rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-800"
          }
        >
          {state.ok ? (
            <CheckCircle2 className="mt-0.5 h-4 w-4 shrink-0" aria-hidden />
          ) : (
            <AlertCircle className="mt-0.5 h-4 w-4 shrink-0" aria-hidden />
          )}
          <span>{state.message}</span>
        </div>
      )}
      <Field
        name="current"
        label="Current password"
        autoComplete="current-password"
        errors={state.fieldErrors?.current}
      />
      <Field
        name="next"
        label="New password"
        autoComplete="new-password"
        errors={state.fieldErrors?.next}
      />
      <Field
        name="confirm"
        label="Confirm new password"
        autoComplete="new-password"
        errors={state.fieldErrors?.confirm}
      />
      <Button type="submit" disabled={pending} className="w-full">
        {pending ? (
          <>
            <Loader2 className="h-4 w-4 animate-spin" aria-hidden />
            Updating password…
          </>
        ) : (
          "Update password"
        )}
      </Button>
    </form>
  );
}

function Field({
  name,
  label,
  autoComplete,
  errors,
}: {
  name: string;
  label: string;
  autoComplete: string;
  errors?: string[];
}) {
  return (
    <label className="block">
      <span className="mb-1.5 block text-sm font-semibold text-[var(--color-ink)]">
        {label}
      </span>
      <div className="relative">
        <Lock
          className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-[var(--color-slate)]"
          aria-hidden
        />
        <input
          name={name}
          type="password"
          required
          autoComplete={autoComplete}
          minLength={name === "current" ? 1 : 10}
          className="w-full rounded-md border border-[var(--color-line)] bg-white py-2.5 pl-9 pr-3 text-[var(--color-ink)] focus:border-[var(--color-blue)] focus:outline-none focus:ring-2 focus:ring-[var(--color-blue)]/20"
        />
      </div>
      {errors && errors.length > 0 && (
        <ul className="mt-1 list-disc pl-5 text-[12px] text-red-700">
          {errors.map((e) => (
            <li key={e}>{e}</li>
          ))}
        </ul>
      )}
    </label>
  );
}
