import { CategoryIcon } from '@/site/components/art/category-icon';
import { ContourArt } from '@/site/components/art/contour-art';
import { hasFacePoint } from '@/site/components/art/face-map';
import { FactIcon } from '@/site/components/art/fact-icon';
import { ZoneArt } from '@/site/components/art/zone-art';
import { MediaSlot } from '@/site/components/media/media-slot';
import { Reveal } from '@/site/components/motion/reveal';
import { StickySteps } from '@/site/components/motion/sticky-steps';
import { TextReveal } from '@/site/components/motion/text-reveal';
import { CardGrid } from '@/site/components/sections/card-grid';
import { TreatmentCard } from '@/site/components/sections/cards';
import { CtaBand } from '@/site/components/sections/cta-band';
import {
    heroFrame,
    PageHero,
    sheet,
} from '@/site/components/sections/page-hero';
import { SectionNav } from '@/site/components/sections/section-nav';
import { ButtonAnchor, ButtonLink } from '@/site/components/ui/button';
import { FaqList } from '@/site/components/ui/faq-list';
import { Eyebrow, SectionHeading } from '@/site/components/ui/heading';
import { Alert, Check, Shield } from '@/site/components/ui/icons';
import { SiteLink } from '@/site/components/ui/link';
import { useShared } from '@/site/hooks/use-shared';
import { formatDate, useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';
import type {
    TreatmentCard as TreatmentCardData,
    TreatmentDetail,
} from '@/site/types';

type TreatmentPageProps = {
    treatment: TreatmentDetail;
    related: TreatmentCardData[];
    /** Contact page with this treatment preselected. */
    contactUrl: string;
};

/**
 * A treatment page tells one story from top to bottom: what it is, who it
 * is for, how it is done (pinned step-by-step scene), what to do
 * afterwards, the possible side effects, the results and the questions.
 *
 * Every section is plain content in the HTML; the scroll effects only
 * change how it is revealed.
 */
export default function TreatmentPage({
    treatment,
    related,
    contactUrl,
}: TreatmentPageProps) {
    const { locale } = useShared();
    const t = useT();
    const { category } = treatment;
    const [opening, ...paragraphs] = treatment.what;

    const sections = [
        {
            id: 'what',
            label: t.treatment.sections.what,
            show: treatment.what.length > 0,
        },
        {
            id: 'who',
            label: t.treatment.sections.who,
            show: treatment.who.length > 0,
        },
        {
            id: 'steps',
            label: t.treatment.sections.steps,
            show: treatment.steps.length > 0,
        },
        {
            id: 'aftercare',
            label: t.treatment.sections.aftercare,
            show: treatment.aftercare.length > 0,
        },
        {
            id: 'risks',
            label: t.treatment.sections.risks,
            show: treatment.risks.length > 0,
        },
        {
            id: 'results',
            label: t.treatment.sections.results,
            show: treatment.results !== '',
        },
        {
            id: 'faq',
            label: t.treatment.sections.faq,
            show: treatment.faq.length > 0,
        },
    ].filter((section) => section.show);

    return (
        <div data-accent={category.accent}>
            <PageHero
                accent={category.accent}
                title={treatment.name}
                lead={treatment.lead}
                visual={
                    <MediaSlot
                        media={treatment.media}
                        priority
                        className={heroFrame}
                        fallback={
                            // Where it is applied, when that is one place of
                            // the face; the page's own contours otherwise.
                            treatment.zone !== null &&
                            hasFacePoint(treatment.zone) ? (
                                <div className="mx-auto aspect-[4/5] max-h-[72svh]">
                                    <ZoneArt zone={treatment.zone} />
                                </div>
                            ) : (
                                <ContourArt seed={treatment.key} alive />
                            )
                        }
                    />
                }
                eyebrow={
                    <SiteLink
                        href={category.url}
                        className="inline-flex items-center gap-2.5 transition-opacity hover:opacity-80"
                    >
                        <CategoryIcon name={category.icon} className="size-5" />
                        <span lang={category.lang ?? undefined}>
                            {category.name}
                        </span>
                    </SiteLink>
                }
            >
                <div className="flex flex-wrap gap-4">
                    <ButtonLink href={contactUrl} size="lg">
                        {t.common.appointmentLong}
                    </ButtonLink>
                    {treatment.steps.length > 0 ? (
                        <ButtonAnchor
                            href="#steps"
                            size="lg"
                            variant="outline"
                            arrow={false}
                        >
                            {t.treatment.sections.steps}
                        </ButtonAnchor>
                    ) : null}
                </div>
            </PageHero>

            {/*
             * Quick facts + in-page navigation. The strip continues the
             * hero (it sits on the hero's own room for the sheet); the
             * links follow the reader down the page (SectionNav).
             */}
            <section
                aria-label={t.treatment.factsLabel}
                className="stage-dark border-line relative z-10 -mt-10 border-t"
            >
                <div className="shell pt-10 pb-20 md:pt-12 md:pb-22">
                    {treatment.facts.length > 0 ? (
                        <dl className="grid grid-cols-2 gap-x-8 gap-y-8 md:grid-cols-3 xl:grid-cols-6">
                            {treatment.facts.map((fact, index) => (
                                <Reveal
                                    key={fact.key}
                                    delay={index * 60}
                                    className="border-line flex flex-col gap-2 border-s ps-5"
                                >
                                    <dt className="text-eyebrow text-soft flex flex-col gap-3 font-semibold uppercase">
                                        <FactIcon
                                            name={fact.key}
                                            className="text-accent-fg size-7"
                                        />
                                        {t.treatment.facts[fact.key]}
                                    </dt>
                                    <dd className="text-lg leading-snug font-medium">
                                        {fact.value}
                                    </dd>
                                </Reveal>
                            ))}
                        </dl>
                    ) : null}

                    <SectionNav
                        sections={sections}
                        label={t.treatment.onThisPage}
                        className="mt-10"
                    />
                </div>
            </section>

            {/* What is it? */}
            {opening ? (
                <section
                    id="what"
                    className={cx(
                        'stage-light section-y scroll-mt-(--header-h)',
                        sheet,
                    )}
                >
                    <div className="shell grid gap-10 lg:grid-cols-12">
                        <div className="lg:col-span-3">
                            <Reveal>
                                <Eyebrow as="h2">
                                    {t.treatment.sections.what}
                                </Eyebrow>
                            </Reveal>
                        </div>
                        <div className="lg:col-span-9">
                            <TextReveal
                                text={opening}
                                className="font-display text-display-4 leading-[1.24]"
                            />
                            {paragraphs.map((paragraph, index) => (
                                <Reveal key={index} delay={80}>
                                    <p className="text-soft mt-10 max-w-3xl text-lg">
                                        {paragraph}
                                    </p>
                                </Reveal>
                            ))}
                        </div>
                    </div>
                </section>
            ) : null}

            {/* Who is it for? */}
            {treatment.who.length > 0 ? (
                <section
                    id="who"
                    className="stage-light section-y scroll-mt-(--header-h) pt-0!"
                >
                    <div className="shell grid gap-10 lg:grid-cols-12">
                        <div className="lg:col-span-3">
                            <Reveal>
                                <Eyebrow as="h2">
                                    {t.treatment.sections.who}
                                </Eyebrow>
                            </Reveal>
                        </div>
                        <ul className="border-line bg-line grid gap-px overflow-hidden rounded-xl border sm:grid-cols-2 lg:col-span-9">
                            {treatment.who.map((item, index) => (
                                <Reveal
                                    as="li"
                                    key={item}
                                    delay={(index % 2) * 80}
                                    // An odd last item spans both columns, so no empty cell is left.
                                    className="bg-stage-2 flex gap-4 p-6 last:odd:sm:col-span-2 md:p-8"
                                >
                                    <Check className="text-accent-fg mt-1 size-5 shrink-0" />
                                    <span className="text-[1.0625rem]">
                                        {item}
                                    </span>
                                </Reveal>
                            ))}
                        </ul>
                    </div>
                </section>
            ) : null}

            {/* How is it done? — pinned step-by-step scene */}
            {treatment.steps.length > 0 ? (
                <section
                    id="steps"
                    className="stage-dark section-y scroll-mt-(--header-h)"
                >
                    <div className="shell">
                        <StickySteps
                            steps={treatment.steps}
                            stepLabel={t.treatment.stepLabel}
                            aside={
                                <SectionHeading
                                    eyebrow={treatment.name}
                                    title={t.treatment.sections.steps}
                                />
                            }
                        />
                    </div>
                </section>
            ) : null}

            {/* Aftercare + possible side effects */}
            {treatment.aftercare.length > 0 || treatment.risks.length > 0 ? (
                <section className="stage-light section-y">
                    <div className="shell grid gap-6 lg:grid-cols-2">
                        {treatment.aftercare.length > 0 ? (
                            <Reveal
                                as="article"
                                className="border-line bg-stage-2 rounded-xl border p-8 md:p-12"
                            >
                                <Shield className="text-accent-fg size-9" />
                                <h2
                                    id="aftercare"
                                    className="font-display text-display-4 mt-8 scroll-mt-[calc(var(--header-h)+6rem)]"
                                >
                                    {t.treatment.sections.aftercare}
                                </h2>
                                <ul className="mt-8 space-y-5">
                                    {treatment.aftercare.map((item) => (
                                        <li key={item} className="flex gap-4">
                                            <Check className="text-accent-fg mt-1 size-5 shrink-0" />
                                            <span>{item}</span>
                                        </li>
                                    ))}
                                </ul>
                            </Reveal>
                        ) : null}

                        {treatment.risks.length > 0 ? (
                            <Reveal
                                as="article"
                                delay={100}
                                className="border-line bg-stage-3 rounded-xl border p-8 md:p-12"
                            >
                                <Alert className="text-accent-fg size-9" />
                                <h2
                                    id="risks"
                                    className="font-display text-display-4 mt-8 scroll-mt-[calc(var(--header-h)+6rem)]"
                                >
                                    {t.treatment.sections.risks}
                                </h2>
                                <ul className="mt-8 space-y-5">
                                    {treatment.risks.map((item) => (
                                        <li key={item} className="flex gap-4">
                                            <span
                                                aria-hidden="true"
                                                className="bg-accent-fg mt-2.5 size-1.5 shrink-0 rounded-full"
                                            />
                                            <span>{item}</span>
                                        </li>
                                    ))}
                                </ul>
                            </Reveal>
                        ) : null}
                    </div>
                </section>
            ) : null}

            {/* Results */}
            {treatment.results ? (
                <section
                    id="results"
                    className="stage-light section-y scroll-mt-(--header-h) pt-0!"
                >
                    <div className="shell grid gap-10 lg:grid-cols-12">
                        <div className="lg:col-span-3">
                            <Reveal>
                                <Eyebrow as="h2">
                                    {t.treatment.sections.results}
                                </Eyebrow>
                            </Reveal>
                        </div>
                        <Reveal className="lg:col-span-9" delay={80}>
                            <p className="font-display max-w-4xl text-[clamp(1.5rem,1.2rem+1vw,2.125rem)] leading-[1.3]">
                                {treatment.results}
                            </p>
                        </Reveal>
                    </div>
                </section>
            ) : null}

            {/* Questions */}
            {treatment.faq.length > 0 ? (
                <section
                    id="faq"
                    className="stage-light section-y scroll-mt-(--header-h) pt-0!"
                >
                    <div className="shell grid gap-10 lg:grid-cols-12">
                        <div className="lg:col-span-3">
                            <Reveal>
                                <Eyebrow as="h2">
                                    {t.treatment.sections.faq}
                                </Eyebrow>
                            </Reveal>
                        </div>
                        <div className="lg:col-span-9">
                            <FaqList
                                items={treatment.faq}
                                name={`faq-${treatment.key}`}
                            />

                            {/* Medical note, review and freshness */}
                            <div className="text-soft mt-12 flex flex-col gap-3 text-sm">
                                <p className="max-w-3xl">
                                    {t.treatment.disclaimer}
                                </p>
                                <p className="flex flex-wrap gap-x-6 gap-y-1">
                                    {treatment.review ? (
                                        <span>
                                            {t.treatment.reviewed(
                                                treatment.review.by,
                                            )}
                                            {treatment.review.at
                                                ? ` · ${formatDate(treatment.review.at, locale)}`
                                                : ''}
                                        </span>
                                    ) : null}
                                    {treatment.updatedAt ? (
                                        <span>
                                            {t.treatment.updated(
                                                formatDate(
                                                    treatment.updatedAt,
                                                    locale,
                                                ),
                                            )}
                                        </span>
                                    ) : null}
                                </p>
                            </div>
                        </div>
                    </div>
                </section>
            ) : null}

            {/* Related treatments */}
            {related.length > 0 ? (
                <section className="stage-light border-line section-y border-t">
                    <div className="shell">
                        <div className="flex flex-wrap items-end justify-between gap-8">
                            <SectionHeading
                                eyebrow={t.treatment.relatedEyebrow}
                                title={t.treatment.relatedTitle}
                                size="display-4"
                            />
                            <Reveal>
                                <ButtonLink
                                    href={category.url}
                                    variant="outline"
                                >
                                    {category.name}
                                </ButtonLink>
                            </Reveal>
                        </div>
                        <CardGrid
                            items={related}
                            itemKey={(item) => item.key}
                            className="mt-12"
                        >
                            {(item) => (
                                <TreatmentCard
                                    treatment={item}
                                    showCategory={false}
                                />
                            )}
                        </CardGrid>
                    </div>
                </section>
            ) : null}

            <CtaBand
                accent={category.accent}
                title={t.treatment.cta.title}
                text={t.treatment.cta.text}
                href={contactUrl}
            />
        </div>
    );
}
