"use client";

import Link from "next/link";
import type { CatalogData, PdfArchiveEntry } from "@/lib/types";
import { formatDate } from "@/lib/format";

export function OverviewClient({
  catalog,
  archives,
}: {
  catalog: CatalogData;
  archives: PdfArchiveEntry[];
}) {
  const demoCount = catalog.features.filter((f) => f.isDemo).length;
  const screenshotCount = catalog.features.reduce(
    (sum, f) => sum + f.screenshots.length,
    0
  );

  return (
    <div className="space-y-8">
      <header className="relative overflow-hidden rounded-2xl border border-[var(--line)] bg-[var(--panel)] px-6 py-8 sm:px-8">
        <div className="pointer-events-none absolute inset-0 bg-[radial-gradient(circle_at_top_right,rgba(37,99,235,0.12),transparent_45%),linear-gradient(180deg,#f8fafc,transparent)]" />
        <div className="relative max-w-2xl">
          <div className="text-sm font-medium tracking-wide text-[var(--accent)]">
            {catalog.settings.companyName}
          </div>
          <h1 className="mt-2 text-3xl font-semibold tracking-tight text-[var(--ink)] sm:text-4xl">
            {catalog.settings.title}
          </h1>
          <p className="mt-3 text-[var(--muted)]">
            {catalog.settings.subtitle ||
              "Build, annotate, and export a professional feature catalog PDF — all stored locally on this machine."}
          </p>
          <div className="mt-6 flex flex-wrap gap-3">
            <Link href="/features/new" className="btn-primary">
              Add feature
            </Link>
            <Link href="/export" className="btn-secondary">
              Generate PDF
            </Link>
          </div>
        </div>
      </header>

      <section className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
        <Stat label="Features" value={String(catalog.features.length)} />
        <Stat label="Categories" value={String(catalog.categories.length)} />
        <Stat label="Screenshots" value={String(screenshotCount)} />
        <Stat label="Saved PDFs" value={String(archives.length)} />
      </section>

      {demoCount > 0 && (
        <section className="panel border-amber-200 bg-amber-50/60 p-4 text-sm text-amber-950">
          <strong>{demoCount} DEMO EXAMPLE features</strong> are included so you
          can explore editing, annotation, preview, and PDF export immediately.
          They are clearly labeled and can be edited or deleted anytime.
        </section>
      )}

      <section className="grid gap-4 lg:grid-cols-2">
        <div className="panel p-5">
          <h2 className="font-semibold text-[var(--ink)]">Recent features</h2>
          <ul className="mt-3 space-y-2">
            {[...catalog.features]
              .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))
              .slice(0, 5)
              .map((f) => (
                <li key={f.id}>
                  <Link
                    href={`/features/${f.id}`}
                    className="flex items-center justify-between gap-3 rounded-lg px-2 py-2 hover:bg-[var(--wash)]"
                  >
                    <span className="truncate font-medium text-[var(--ink)]">
                      {f.name}
                    </span>
                    <span className="shrink-0 text-xs text-[var(--muted)]">
                      {f.screenshots.length} shots
                    </span>
                  </Link>
                </li>
              ))}
          </ul>
        </div>
        <div className="panel p-5">
          <h2 className="font-semibold text-[var(--ink)]">Latest exports</h2>
          {archives.length === 0 ? (
            <p className="mt-3 text-sm text-[var(--muted)]">
              No PDFs generated yet.
            </p>
          ) : (
            <ul className="mt-3 space-y-2">
              {archives.slice(0, 5).map((a) => (
                <li key={a.id}>
                  <a
                    href={`/api/pdfs/${a.id}?inline=1`}
                    target="_blank"
                    rel="noreferrer"
                    className="flex items-center justify-between gap-3 rounded-lg px-2 py-2 hover:bg-[var(--wash)]"
                  >
                    <span className="truncate font-medium text-[var(--ink)]">
                      {a.title}
                    </span>
                    <span className="shrink-0 text-xs text-[var(--muted)]">
                      {a.pageCount}p · {formatDate(a.createdAt)}
                    </span>
                  </a>
                </li>
              ))}
            </ul>
          )}
        </div>
      </section>
    </div>
  );
}

function Stat({ label, value }: { label: string; value: string }) {
  return (
    <div className="panel px-4 py-4">
      <div className="text-xs uppercase tracking-wide text-[var(--muted)]">
        {label}
      </div>
      <div className="mt-1 text-2xl font-semibold text-[var(--ink)]">{value}</div>
    </div>
  );
}
