import Link from "next/link";
import {
  attributionPackages,
  type LegalDocument,
  legalDocuments,
} from "@/static/legal";

type LegalDocumentViewProps = {
  document: LegalDocument;
};

const LegalDocumentView = ({ document }: LegalDocumentViewProps) => {
  const showAttribution = document.slug === "attribution";

  return (
    <div className="bg-[#F7F6FC] text-foreground">
      <div className="container py-10 sm:py-14 lg:py-16">
        <nav aria-label="Breadcrumb" className="mb-6 text-sm text-[#5C5878]">
          <ol className="flex flex-wrap items-center gap-2">
            <li>
              <Link
                href="/legal"
                className="font-medium text-brand transition-colors hover:text-brand-hover"
              >
                Legal Center
              </Link>
            </li>
            <li aria-hidden="true">/</li>
            <li className="text-[#3D3A55]">{document.title}</li>
          </ol>
        </nav>

        <div className="grid gap-10 lg:grid-cols-[minmax(0,1fr)_240px] lg:gap-14 xl:grid-cols-[minmax(0,1fr)_280px]">
          <article className="rounded-2xl border border-[#E6E3F2] bg-white px-5 py-7 sm:px-8 sm:py-9 lg:px-10">
            <header className="border-b border-[#E6E3F2] pb-6">
              <h1 className="text-[clamp(1.85rem,4vw,2.75rem)] font-bold leading-[1.15] text-ink">
                {document.title}
              </h1>
              <p className="mt-3 max-w-3xl text-base leading-relaxed text-[#3D3A55] sm:text-lg">
                {document.description}
              </p>
              <p className="mt-4 text-sm text-[#5C5878]">
                Last updated: {document.updatedAt}
              </p>
            </header>

            <div className="mt-8 space-y-8 sm:mt-10 sm:space-y-10">
              {document.sections.map((section) => (
                <section key={section.heading}>
                  <h2 className="text-xl font-bold text-ink sm:text-2xl">
                    {section.heading}
                  </h2>
                  <div className="mt-3 space-y-3 sm:mt-4">
                    {section.paragraphs.map((paragraph) => (
                      <p
                        key={paragraph}
                        className="text-base leading-relaxed text-[#3D3A55]"
                      >
                        {paragraph}
                      </p>
                    ))}
                  </div>
                  {section.bullets ? (
                    <ul className="mt-4 list-disc space-y-2 pl-5 text-base leading-relaxed text-[#3D3A55]">
                      {section.bullets.map((bullet) => (
                        <li key={bullet}>{bullet}</li>
                      ))}
                    </ul>
                  ) : null}
                </section>
              ))}

              {showAttribution ? (
                <section>
                  <h2 className="text-xl font-bold text-ink sm:text-2xl">
                    Packages
                  </h2>
                  <ul className="mt-5 divide-y divide-[#E6E3F2] overflow-hidden rounded-xl border border-[#E6E3F2]">
                    {attributionPackages.map((pkg) => (
                      <li
                        key={pkg.name}
                        className="flex flex-col gap-1 bg-white px-4 py-4 sm:flex-row sm:items-center sm:justify-between sm:gap-4 sm:px-5"
                      >
                        <div>
                          <p className="font-semibold text-ink">
                            {pkg.name}
                          </p>
                          {pkg.note ? (
                            <p className="mt-1 text-sm text-[#5C5878]">
                              {pkg.note}
                            </p>
                          ) : null}
                        </div>
                        <div className="flex items-center gap-3 text-sm">
                          <span className="rounded-full bg-[#F3F1FB] px-2.5 py-1 font-medium text-brand">
                            {pkg.license}
                          </span>
                          <a
                            href={pkg.url}
                            target="_blank"
                            rel="noreferrer"
                            className="font-medium text-foreground underline decoration-foreground/25 underline-offset-2 transition-colors hover:text-brand"
                          >
                            Source
                          </a>
                        </div>
                      </li>
                    ))}
                  </ul>
                </section>
              ) : null}
            </div>
          </article>

          <aside className="lg:pt-2">
            <div className="sticky top-24 rounded-2xl border border-[#E6E3F2] bg-white p-5">
              <h2 className="text-sm font-semibold tracking-[0.06em] text-[#5C5878] uppercase">
                Legal documents
              </h2>
              <ul className="mt-4 flex flex-col gap-2.5">
                {legalDocuments.map((doc) => {
                  const isActive = doc.slug === document.slug;

                  return (
                    <li key={doc.slug}>
                      <Link
                        href={`/legal/${doc.slug}`}
                        className={
                          isActive
                            ? "font-semibold text-brand"
                            : "text-foreground transition-colors hover:text-brand"
                        }
                        aria-current={isActive ? "page" : undefined}
                      >
                        {doc.title}
                      </Link>
                    </li>
                  );
                })}
              </ul>
            </div>
          </aside>
        </div>
      </div>
    </div>
  );
};

export default LegalDocumentView;
