"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { FileText } from "lucide-react";
import ImmersivePage from "@/components/immersive/ImmersivePage";

const LEGAL_DOCS = [
  { label: "Terms & Conditions", href: "/terms" },
  { label: "Privacy Policy", href: "/privacy" },
  { label: "Cookies Policy", href: "/cookies" },
  { label: "Refund Policy", href: "/refund" },
] as const;

export type LegalSection = { id: string; label: string };

type LegalPageLayoutProps = {
  title: string;
  effectiveDate?: string;
  sections: LegalSection[];
  children: React.ReactNode;
};

/**
 * Shared frame for the four legal documents, on the common immersive shell:
 * a sticky document/section sidebar beside the prose card. The `.legal-*`
 * and `.legal-prose` classes resolve against the immersive tokens, so the
 * copy follows the dark/light toggle with the rest of the site.
 */
export function LegalPageLayout({
  title,
  effectiveDate,
  sections,
  children,
}: LegalPageLayoutProps) {
  const pathname = usePathname();

  return (
    <ImmersivePage>
      <div className="mx-auto flex w-full max-w-[88rem] flex-col gap-8 px-6 pt-14 lg:flex-row lg:gap-12 lg:px-12 lg:pt-20">
        {/* Sidebar — sticky on desktop */}
        <aside className="order-2 shrink-0 lg:order-1 lg:w-56 xl:w-64">
          <div className="immersive-card sticky top-24 space-y-8 p-5">
            <div>
              <p className="immersive-label immersive-label-accent mb-3">
                Legal documents
              </p>
              <nav className="space-y-0.5">
                {LEGAL_DOCS.map((doc) => {
                  const isActive = pathname === doc.href;
                  return (
                    <Link
                      key={doc.href}
                      href={doc.href}
                      className={`flex items-center gap-2 rounded-lg px-3 py-2 text-sm font-medium transition-colors ${
                        isActive
                          ? "bg-[hsl(var(--primary))]/15 text-[hsl(var(--primary))]"
                          : "text-[hsl(var(--muted-foreground))] hover:bg-[hsl(var(--primary))]/[0.06] hover:text-[hsl(var(--foreground))]"
                      }`}
                    >
                      <FileText size={14} className="shrink-0 opacity-70" />
                      {doc.label}
                    </Link>
                  );
                })}
              </nav>
            </div>

            {sections.length > 0 && (
              <div>
                <p className="immersive-label immersive-label-accent mb-3">
                  On this page
                </p>
                <nav className="space-y-0.5">
                  {sections.map((section) => (
                    <a
                      key={section.id}
                      href={`#${section.id}`}
                      className="block rounded-lg px-3 py-2 text-sm text-[hsl(var(--muted-foreground))] transition-colors hover:bg-[hsl(var(--primary))]/[0.06] hover:text-[hsl(var(--foreground))]"
                    >
                      {section.label}
                    </a>
                  ))}
                </nav>
              </div>
            )}
          </div>
        </aside>

        {/* Document */}
        <main className="order-1 min-w-0 flex-1 lg:order-2">
          <article className="immersive-card px-6 py-8 sm:px-8 sm:py-10 lg:px-10">
            <div className="mb-8">
              <Link
                href="/"
                className="inline-flex items-center text-sm font-medium text-[hsl(var(--primary))] transition-colors hover:text-[hsl(var(--primary-hover))]"
              >
                ← Back to Home
              </Link>
            </div>
            <h1 className="font-heading mb-2 text-3xl font-medium tracking-tight sm:text-4xl">
              {title}
            </h1>
            {effectiveDate && (
              <p className="mb-10 text-sm text-[hsl(var(--muted-foreground))]">
                {effectiveDate}
              </p>
            )}
            <div className="legal-prose prose prose-slate max-w-none prose-headings:scroll-mt-24 prose-a:no-underline hover:prose-a:underline">
              {children}
            </div>
          </article>
        </main>
      </div>
    </ImmersivePage>
  );
}
