"use client";

import { useActionState, useEffect } from "react";
import { ArrowRight } from "lucide-react";
import { toast } from "sonner";
import { subscribeAction, type NewsletterState } from "@/app/actions/newsletter";
import { cn } from "@/lib/utils";

const initialState: NewsletterState = { status: "idle" };

export function NewsletterForm({
  locale,
  labels,
  variant = "light",
  className,
  compact = false,
}: {
  locale: string;
  labels: { placeholder: string; cta: string; success: string; privacy?: string };
  variant?: "light" | "dark";
  className?: string;
  compact?: boolean;
}) {
  const [state, formAction, pending] = useActionState(subscribeAction, initialState);

  useEffect(() => {
    if (state.status === "success") toast.success(state.message ?? labels.success);
    if (state.status === "error" && state.message) toast.error(state.message);
  }, [state, labels.success]);

  const dark = variant === "dark";

  return (
    <div className={cn("w-full", className)}>
      {state.status === "success" ? (
        <p className={cn("text-sm", dark ? "text-ivory/80" : "text-olive")}>
          {state.message ?? labels.success}
        </p>
      ) : (
        <form action={formAction} className="w-full">
          <input type="hidden" name="locale" value={locale} />
          <div
            className={cn(
              "flex items-center border-b",
              dark ? "border-ivory/30" : "border-charcoal/30",
            )}
          >
            <label htmlFor={`newsletter-${locale}-${variant}-${compact ? "c" : "f"}`} className="sr-only">
              {labels.placeholder}
            </label>
            <input
              id={`newsletter-${locale}-${variant}-${compact ? "c" : "f"}`}
              type="email"
              name="email"
              required
              autoComplete="email"
              placeholder={labels.placeholder}
              className={cn(
                "h-12 w-full min-w-0 bg-transparent text-sm outline-none placeholder:text-current/40",
                dark ? "text-ivory placeholder:text-ivory/40" : "text-charcoal placeholder:text-charcoal/40",
              )}
            />
            <button
              type="submit"
              disabled={pending}
              aria-label={labels.cta}
              className={cn(
                "flex h-12 shrink-0 items-center gap-2 px-2 text-[12px] uppercase tracking-[0.16em] transition-opacity disabled:opacity-50",
                dark ? "text-ivory" : "text-charcoal",
              )}
            >
              {pending ? "…" : labels.cta}
              <ArrowRight className="h-3.5 w-3.5 rtl:rotate-180" />
            </button>
          </div>
          {state.status === "error" && state.message && (
            <p className={cn("mt-2 text-[13px]", dark ? "text-sand" : "text-destructive")}>{state.message}</p>
          )}
          {labels.privacy && !compact && (
            <p className={cn("mt-2.5 text-[12px]", dark ? "text-ivory/35" : "text-charcoal/40")}>
              {labels.privacy}
            </p>
          )}
        </form>
      )}
    </div>
  );
}
