import { useRef } from 'react';
import { CategoryIcon } from '@/site/components/art/category-icon';
import { hasCategoryScene } from '@/site/components/art/category-scenes';
import { ContourArt } from '@/site/components/art/contour-art';
import { MediaSlot } from '@/site/components/media/media-slot';
import { Reveal } from '@/site/components/motion/reveal';
import { TextReveal } from '@/site/components/motion/text-reveal';
import { CardGrid } from '@/site/components/sections/card-grid';
import { CategoryCard, 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 { TreatmentStory } from '@/site/components/sections/treatment-story';
import { supportsZoneMap, ZoneMap } from '@/site/components/sections/zone-map';
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 { RailControls } from '@/site/components/ui/rail-controls';
import { useRail } from '@/site/hooks/use-rail';
import { useShared } from '@/site/hooks/use-shared';
import { useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';
import type {
    CategoryCard as CategoryCardData,
    CategoryDetail,
} from '@/site/types';

type CategoryPageProps = {
    category: CategoryDetail;
    others: CategoryCardData[];
};

/**
 * Hub page of one area of expertise: what it is about, what stands out,
 * every treatment in it, common questions and the other areas.
 *
 * The treatments are told beside a drawing: the face map where they have
 * zones on the face (botox, fillers), the area's own scene otherwise —
 * pinned while the list scrolls and moving on with it (TreatmentStory).
 * An area without a scene falls back to a grid of cards.
 */
export default function CategoryPage({ category, others }: CategoryPageProps) {
    const { nav } = useShared();
    const t = useT();
    // The row of other areas and its previous / next buttons.
    const railRef = useRef<HTMLUListElement>(null);
    const rail = useRail(railRef);

    const [opening, ...paragraphs] = category.intro;
    const withMap = supportsZoneMap(category.treatments);
    const withScene =
        hasCategoryScene(category.key) && category.treatments.length > 1;

    return (
        <div data-accent={category.accent}>
            <PageHero
                accent={category.accent}
                eyebrow={nav.pages.treatments.label}
                title={category.name}
                lead={category.tagline}
                visual={
                    <MediaSlot
                        media={category.media}
                        priority
                        className={heroFrame}
                        fallback={
                            <div className="relative">
                                <ContourArt seed={category.key} alive />
                                <CategoryIcon
                                    name={category.icon}
                                    className="absolute inset-0 m-auto size-20 text-accent md:size-24"
                                />
                            </div>
                        }
                    />
                }
            >
                <div className="flex flex-wrap items-center gap-6">
                    <ButtonAnchor href="#treatments" size="lg">
                        {t.common.treatmentCount(category.count)}
                    </ButtonAnchor>
                    <ButtonLink href={nav.pages.contact.url} variant="ghost">
                        {t.common.appointmentLong}
                    </ButtonLink>
                </div>
            </PageHero>

            {/* Overview */}
            {opening ? (
                <section className={cx('stage-light section-y', sheet)}>
                    <div className="shell">
                        <Reveal>
                            <Eyebrow>{t.category.introEyebrow}</Eyebrow>
                        </Reveal>
                        <TextReveal
                            text={opening}
                            className="font-display text-display-4 mt-10 max-w-5xl leading-[1.22]"
                        />
                        {paragraphs.length > 0 ? (
                            <div className="text-soft mt-12 grid max-w-5xl gap-8 text-lg md:grid-cols-2 md:gap-12">
                                {paragraphs.map((paragraph, index) => (
                                    <Reveal key={index} delay={index * 80}>
                                        <p>{paragraph}</p>
                                    </Reveal>
                                ))}
                            </div>
                        ) : null}

                        {category.highlights.length > 0 ? (
                            <ul className="border-line bg-line mt-20 grid gap-px overflow-hidden rounded-xl border md:grid-cols-3">
                                {category.highlights.map((highlight, index) => (
                                    <Reveal
                                        as="li"
                                        key={highlight.title}
                                        delay={index * 90}
                                        className="bg-stage-2 p-8 md:p-10"
                                    >
                                        <p
                                            aria-hidden="true"
                                            className="font-display text-accent-fg text-3xl tabular-nums"
                                        >
                                            {String(index + 1).padStart(2, '0')}
                                        </p>
                                        <h2 className="font-display mt-8 text-2xl">
                                            {highlight.title}
                                        </h2>
                                        <p className="text-soft mt-3">
                                            {highlight.text}
                                        </p>
                                    </Reveal>
                                ))}
                            </ul>
                        ) : null}
                    </div>
                </section>
            ) : null}

            {/* Treatments: beside the face map, beside the area's drawing, or as cards */}
            <section
                id="treatments"
                className={
                    withMap || withScene
                        ? 'stage-dark section-y scroll-mt-(--header-h)'
                        : 'stage-light section-y scroll-mt-(--header-h) pt-0!'
                }
            >
                <div className="shell">
                    {withMap ? (
                        <ZoneMap treatments={category.treatments} />
                    ) : withScene ? (
                        <TreatmentStory
                            category={category.key}
                            treatments={category.treatments}
                        />
                    ) : (
                        <>
                            <SectionHeading
                                eyebrow={t.category.treatmentsEyebrow}
                                title={t.category.treatmentsTitle}
                            />
                            <CardGrid
                                items={category.treatments}
                                itemKey={(treatment) => treatment.key}
                                className="mt-14"
                            >
                                {(treatment) => (
                                    <TreatmentCard
                                        treatment={treatment}
                                        showCategory={false}
                                    />
                                )}
                            </CardGrid>
                        </>
                    )}
                </div>
            </section>

            {/* Questions */}
            {category.faq.length > 0 ? (
                <section className="stage-light section-y">
                    <div className="shell grid gap-12 lg:grid-cols-12">
                        <div className="lg:col-span-4">
                            <SectionHeading
                                eyebrow={t.category.faqEyebrow}
                                title={t.category.faqTitle}
                                size="display-4"
                            />
                        </div>
                        <FaqList
                            items={category.faq}
                            name={`faq-${category.key}`}
                            className="lg:col-span-8"
                        />
                    </div>
                </section>
            ) : null}

            {/* Other areas */}
            {others.length > 0 ? (
                <section
                    data-accent="brand"
                    className="stage-light section-y pt-0!"
                >
                    <div className="shell flex items-end justify-between gap-8">
                        <SectionHeading
                            eyebrow={t.category.othersEyebrow}
                            title={t.category.othersTitle}
                            size="display-4"
                        />
                        <RailControls rail={rail} />
                    </div>
                    {/* Cards are as tall as the tallest one: nothing is cut off. */}
                    <ul ref={railRef} data-hscroll-track="" className="mt-12">
                        {others.map((other) => (
                            <li
                                key={other.key}
                                className="min-h-[24rem] w-[78vw] sm:w-[22rem]"
                            >
                                <CategoryCard category={other} />
                            </li>
                        ))}
                    </ul>
                </section>
            ) : null}

            <CtaBand accent={category.accent} />
        </div>
    );
}
