import Link from "next/link";
import { ArrowLeft } from "lucide-react";
import type { LegalDoc } from "@/lib/legal-content";

export function LegalPage({
  locale,
  doc,
  labels,
  links,
}: {
  locale: string;
  doc: LegalDoc;
  labels: { onThisPage: string; updated: string; backHome: string; contact: string; help: string };
  links: { href: string; label: string }[];
}) {
  return (
    <>
      <section className="border-b border-border bg-cream/40">
        <div className="shell py-12 md:py-16">
          <p className="eyebrow">{doc.eyebrow}</p>
          <h1 className="mt-4 max-w-2xl font-serif text-4xl font-light leading-tight tracking-tight md:text-6xl">
            {doc.title}
          </h1>
          <p className="mt-4 max-w-2xl text-[15px] leading-relaxed text-charcoal/55">{doc.intro}</p>
          <p className="mt-5 text-[12px] uppercase tracking-[0.14em] text-charcoal/35">
            {labels.updated} · {doc.updated}
          </p>
        </div>
      </section>

      <div className="shell grid gap-10 py-12 md:py-16 lg:grid-cols-[240px_1fr] lg:gap-16">
        <aside>
          <div className="lg:sticky lg:top-32">
            <p className="text-[12px] uppercase tracking-[0.16em] text-charcoal/35">{labels.onThisPage}</p>
            <ol className="mt-4 space-y-2.5 border-l border-border pl-4">
              {doc.sections.map((section, index) => (
                <li key={section.heading}>
                  <a
                    href={`#section-${index + 1}`}
                    className="text-[13px] leading-snug text-charcoal/45 transition-colors hover:text-charcoal"
                  >
                    {section.heading}
                  </a>
                </li>
              ))}
            </ol>

            <div className="mt-8 border-t border-border pt-5">
              <p className="text-[12px] uppercase tracking-[0.14em] text-charcoal/35">{labels.help}</p>
              <div className="mt-3 space-y-2">
                {links.map((link) => (
                  <Link
                    key={link.href}
                    href={link.href}
                    className="block text-[12px] text-charcoal/50 hover:text-charcoal"
                  >
                    {link.label}
                  </Link>
                ))}
              </div>
            </div>
          </div>
        </aside>

        <article className="max-w-2xl">
          {doc.sections.map((section, index) => (
            <section key={section.heading} id={`section-${index + 1}`} className="scroll-mt-32 border-b border-border pb-8 pt-8 first:pt-0">
              <div className="flex items-baseline gap-3">
                <span className="font-serif text-sm text-gold">{String(index + 1).padStart(2, "0")}</span>
                <h2 className="font-serif text-2xl font-light text-charcoal">{section.heading}</h2>
              </div>
              <div className="mt-3 space-y-4 pl-7 text-[15px] leading-[1.95] text-charcoal/65">
                {section.paragraphs.map((paragraph, pIndex) => (
                  <p key={pIndex}>{paragraph}</p>
                ))}
              </div>
            </section>
          ))}

          <div className="mt-10 flex flex-wrap items-center justify-between gap-4">
            <Link
              href={`/${locale}`}
              className="inline-flex items-center gap-2 text-[12px] uppercase tracking-[0.14em] text-charcoal/45 hover:text-charcoal"
            >
              <ArrowLeft className="h-3.5 w-3.5 rtl:rotate-180" />
              {labels.backHome}
            </Link>
            <Link
              href={`/${locale}/contact`}
              className="border border-charcoal/20 px-4 py-2 text-[12px] uppercase tracking-[0.14em] text-charcoal/60 hover:border-charcoal hover:text-charcoal"
            >
              {labels.contact}
            </Link>
          </div>
        </article>
      </div>
    </>
  );
}
