import type { ReactNode } from 'react';
import { Reveal } from '@/site/components/motion/reveal';
import { MapArt } from '@/site/components/art/map-art';
import { ContactForm } from '@/site/components/sections/contact-form';
import { ContactMap } from '@/site/components/sections/contact-map';
import type { TreatmentOptions } from '@/site/components/sections/contact-form';
import { CrmForm } from '@/site/components/sections/crm-form';
import { PageHero, sheet } from '@/site/components/sections/page-hero';
import { Clock, Mail, MapPin, Phone } from '@/site/components/ui/icons';
import { WhatsAppButton } from '@/site/components/ui/whatsapp-button';
import { useShared } from '@/site/hooks/use-shared';
import { useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';

type ContactProps = {
    options: TreatmentOptions;
    selected: string | null;
    action: string;
    privacyUrl: string;
    /** Public key of the "I am not a robot" check; null while it is off. */
    turnstile: string | null;
};

/**
 * Contact and appointment page: the request form plus the clinic's contact
 * details. The form is the CRM's (a frame); while the CRM forms are off it
 * is the site's own. Details that are not entered yet (Admin → Site
 * settings) are left out and a short note is shown instead — nothing is
 * invented.
 */
export default function Contact({
    options,
    selected,
    action,
    privacyUrl,
    turnstile,
}: ContactProps) {
    const { site } = useShared();
    const t = useT();
    const { contact } = site;
    const hasDetails = Boolean(
        contact.phone || contact.email || contact.address || contact.hours,
    );

    return (
        <>
            <PageHero
                eyebrow={t.contact.eyebrow}
                title={t.contact.title}
                lead={t.contact.lead}
                art={<MapArt />}
            />

            <section className={cx('stage-light section-y-sm', sheet)}>
                <div className="shell grid gap-10 lg:grid-cols-12 lg:gap-16">
                    <Reveal className="lg:col-span-7">
                        {site.crm ? (
                            <div className="border-line bg-stage-2 rounded-xl border p-6 md:p-10">
                                <h2 className="font-display text-display-4">
                                    {t.contact.form.legend}
                                </h2>
                                <CrmForm
                                    config={site.crm}
                                    kind="message"
                                    className="mt-8"
                                />
                            </div>
                        ) : (
                            <ContactForm
                                action={action}
                                options={options}
                                selected={selected}
                                privacyUrl={privacyUrl}
                                turnstile={turnstile}
                            />
                        )}
                        <p className="text-soft mt-5 text-sm">
                            {t.contact.note}
                        </p>
                    </Reveal>

                    <Reveal as="aside" delay={120} className="lg:col-span-5">
                        <h2 className="font-display text-display-4">
                            {t.contact.info.title}
                        </h2>

                        {hasDetails ? (
                            <address className="mt-8 not-italic">
                                <dl className="divide-line border-line divide-y border-y">
                                    {contact.phone && contact.phoneHref ? (
                                        <Detail
                                            icon={<Phone />}
                                            label={t.contact.info.phone}
                                        >
                                            <a
                                                href={contact.phoneHref}
                                                className="link-underline"
                                            >
                                                {contact.phone}
                                            </a>
                                        </Detail>
                                    ) : null}
                                    {contact.email ? (
                                        <Detail
                                            icon={<Mail />}
                                            label={t.contact.info.email}
                                        >
                                            <a
                                                href={`mailto:${contact.email}`}
                                                className="link-underline"
                                            >
                                                {contact.email}
                                            </a>
                                        </Detail>
                                    ) : null}
                                    {contact.address ? (
                                        <Detail
                                            icon={<MapPin />}
                                            label={t.contact.info.address}
                                        >
                                            <span className="whitespace-pre-line">
                                                {contact.address}
                                            </span>
                                            {contact.mapUrl ? (
                                                <a
                                                    href={contact.mapUrl}
                                                    target="_blank"
                                                    rel="noopener noreferrer"
                                                    className="text-accent-fg mt-2 block text-sm underline underline-offset-4"
                                                >
                                                    {t.contact.info.map}
                                                    <span className="sr-only">
                                                        {' '}
                                                        ({t.common.newTab})
                                                    </span>
                                                </a>
                                            ) : null}
                                        </Detail>
                                    ) : null}
                                    {contact.hours ? (
                                        <Detail
                                            icon={<Clock />}
                                            label={t.contact.info.hours}
                                        >
                                            <span className="whitespace-pre-line">
                                                {contact.hours}
                                            </span>
                                        </Detail>
                                    ) : null}
                                </dl>
                            </address>
                        ) : (
                            <p className="text-soft mt-6 text-lg">
                                {t.contact.info.pending}
                            </p>
                        )}

                        <WhatsAppButton className="mt-8" />
                    </Reveal>
                </div>

                {contact.mapEmbedUrl && contact.address ? (
                    <Reveal className="shell mt-16 md:mt-24">
                        <ContactMap
                            src={contact.mapEmbedUrl}
                            address={contact.address}
                            href={contact.mapUrl}
                        />
                    </Reveal>
                ) : null}
            </section>
        </>
    );
}

function Detail({
    icon,
    label,
    children,
}: {
    icon: ReactNode;
    label: string;
    children: ReactNode;
}) {
    // A group of a <dl> may hold nothing but its <dt> and <dd>: the icon
    // lives inside the term, the value is indented to line up with the label.
    return (
        <div className="py-6">
            <dt className="text-eyebrow text-soft flex items-center gap-5 font-semibold uppercase">
                <span aria-hidden="true" className="text-accent-fg text-xl">
                    {icon}
                </span>
                {label}
            </dt>
            <dd className="mt-2 ps-10 text-lg">{children}</dd>
        </div>
    );
}
