import { Link } from '@inertiajs/react';
import { ArrowRight } from 'lucide-react';
import { contact } from '@/routes';

export type LegalSection = {
    heading: string;
    paragraphs?: string[];
    items?: string[];
};

function sectionAnchor(index: number, heading: string): string {
    const slug = heading
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/(^-|-$)/g, '');

    return `${index + 1}-${slug}`;
}

/**
 * Long-form policy layout: numbered sections with a sticky table of
 * contents on large screens.
 */
export default function LegalDocument({
    sections,
}: {
    sections: LegalSection[];
}) {
    return (
        <section className="px-5 py-14 sm:px-6 lg:px-10 lg:py-20">
            <div className="mx-auto grid w-full max-w-6xl gap-12 lg:grid-cols-[240px_1fr] lg:gap-16">
                <nav aria-label="On this page" className="hidden lg:block">
                    <div className="sticky top-28">
                        <p className="mb-4 text-[10.5px] font-bold tracking-widest text-ink-400 uppercase">
                            On this page
                        </p>
                        <ol className="flex flex-col gap-2.5 border-l border-surface-200 text-[13.5px]">
                            {sections.map((section, index) => (
                                <li key={section.heading}>
                                    <a
                                        href={`#${sectionAnchor(index, section.heading)}`}
                                        className="-ml-px block border-l border-transparent pl-4 text-ink-500 transition-colors hover:border-brand hover:text-ink"
                                    >
                                        {section.heading}
                                    </a>
                                </li>
                            ))}
                        </ol>
                    </div>
                </nav>

                <div className="max-w-[72ch]">
                    <ol className="flex flex-col gap-11">
                        {sections.map((section, index) => (
                            <li
                                key={section.heading}
                                id={sectionAnchor(index, section.heading)}
                            >
                                <h2 className="mb-4 flex gap-3 font-display text-[22px] leading-tight font-semibold tracking-[-0.015em] text-ink">
                                    <span className="text-brand tabular-nums">
                                        {String(index + 1).padStart(2, '0')}
                                    </span>
                                    {section.heading}
                                </h2>
                                <div className="flex flex-col gap-4 text-[15px] leading-[1.75] text-ink-500">
                                    {section.paragraphs?.map((paragraph) => (
                                        <p key={paragraph}>{paragraph}</p>
                                    ))}
                                    {section.items && (
                                        <ul className="flex flex-col gap-2.5">
                                            {section.items.map((item) => (
                                                <li
                                                    key={item}
                                                    className="relative pl-5 before:absolute before:top-[0.7em] before:left-0 before:size-1.5 before:rounded-full before:bg-brand"
                                                >
                                                    {item}
                                                </li>
                                            ))}
                                        </ul>
                                    )}
                                </div>
                            </li>
                        ))}
                    </ol>

                    <div className="mt-14 flex flex-col gap-4 rounded-landing-lg border border-surface-200 bg-surface-50 p-6 sm:flex-row sm:items-center sm:justify-between">
                        <p className="text-[15px] font-semibold text-ink">
                            Questions about this policy?
                        </p>
                        <Link
                            href={contact()}
                            className="inline-flex items-center gap-2 text-sm font-semibold text-brand hover:text-brand-hover"
                        >
                            Contact us
                            <ArrowRight
                                className="size-3.5"
                                aria-hidden="true"
                            />
                        </Link>
                    </div>
                </div>
            </div>
        </section>
    );
}
