import { useEffect, useRef } from 'react';
import { Button, ButtonAnchor } from '@/site/components/ui/button';
import { Check, WhatsApp } from '@/site/components/ui/icons';
import { useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';

type FormDoneProps = {
    /**
     * Address of the WhatsApp chat the stored request leads to, if it leads
     * to one. The chat is opened by whoever stored the request; the button
     * here is for the browser that kept the new tab from opening.
     */
    chat?: string | null;
    /** Show the form again. */
    onAgain: () => void;
    className?: string;
};

/**
 * Shown in place of a request form once its request is stored (the CRM
 * frame, the short form in front of WhatsApp).
 *
 * This is the only place of the site that links to a WhatsApp chat, and it
 * is reached only after a name and a phone number were left: no button or
 * link leads to a WhatsApp number by itself (docs/DECISIONS.md K39).
 */
export function FormDone({ chat = null, onAgain, className }: FormDoneProps) {
    const t = useT();
    const title = useRef<HTMLParagraphElement>(null);

    // The form that had the focus is gone: hand it to the confirmation.
    useEffect(() => {
        title.current?.focus({ preventScroll: true });
    }, []);

    return (
        <div
            role="status"
            className={cx(
                '@container flex flex-col items-start gap-4',
                className,
            )}
        >
            {/* The title follows the width it is given: the panel of the
                floating button is narrow on every screen. */}
            <p
                ref={title}
                tabIndex={-1}
                className="font-display @min-[30rem]:text-display-4 flex items-center gap-3 text-2xl leading-tight outline-none"
            >
                <span className="bg-accent-fg text-stage grid size-9 shrink-0 place-items-center rounded-full">
                    <Check className="size-5" />
                </span>
                {chat ? t.forms.whatsappDoneTitle : t.contact.form.successTitle}
            </p>
            <p className="text-soft">
                {chat ? t.forms.whatsappDoneText : t.contact.form.successText}
            </p>
            <div className="flex flex-wrap items-center gap-3">
                {chat ? (
                    <ButtonAnchor
                        href={chat}
                        target="_blank"
                        rel="noopener noreferrer"
                        variant="whatsapp"
                        arrow={false}
                    >
                        <span className="inline-flex items-center gap-2.5">
                            <WhatsApp className="size-5" />
                            {t.forms.whatsappOpen}
                            <span className="sr-only">({t.common.newTab})</span>
                        </span>
                    </ButtonAnchor>
                ) : null}
                <Button variant="outline" onClick={onAgain}>
                    {t.contact.form.another}
                </Button>
            </div>
        </div>
    );
}
