"use client";

import { useEffect, useRef } from "react";
import { cn } from "cn";
import { CheckCircleIcon } from "@/components/icons";
import { Button } from "@/components/ui/button";
import { outlinePillClass } from "./form-footer";

type FormSuccessProps = { title: string; message: string; againLabel: string; onAgain: () => void; className?: string };

/**
 * Shown in place of a form after it was sent (no Figma design): a check mark in the success green
 * (the same green as the newsletter's "You're subscribed!"), a title, the form's own thank-you text,
 * and "Send another request", which brings back an empty form.
 * It is much shorter than the form it replaces, so it scrolls itself into view and takes the focus.
 */
export function FormSuccess({ title, message, againLabel, onAgain, className }: FormSuccessProps) {
  const panel = useRef<HTMLDivElement>(null);
  useEffect(() => {
    panel.current?.focus({ preventScroll: true });
    panel.current?.scrollIntoView({ block: "center", behavior: "smooth" });
  }, []);

  return (
    <div
      ref={panel}
      role="status"
      tabIndex={-1}
      className={cn("flex scroll-mt-28 flex-col items-center gap-4 px-5 py-12 text-center outline-none md:py-16", className)}
    >
      <span className="flex size-16 items-center justify-center rounded-full bg-accent-mint/15 text-accent-mint">
        <CheckCircleIcon className="size-8 shrink-0" />
      </span>
      <h3 className="font-heading text-2xl font-bold text-white">{title}</h3>
      <p className="max-w-md text-base leading-7 text-soft">{message}</p>
      <Button type="button" variant="outline" onClick={onAgain} className={cn(outlinePillClass, "mt-2")}>
        {againLabel}
      </Button>
    </div>
  );
}
