import Link from "next/link";
import { listDocuments, readDocument } from "@/lib/wiki";
import { ChatBubble } from "@/components/chat-bubble";
import { AuthBar } from "@/components/auth-bar";
import { BatchProgressBanner } from "@/components/batch-progress-banner";
import { getLocale } from "@/components/locale-toggle";
import { summarize, pickPitch } from "@/lib/doc-summary";
import { DocListFilters } from "@/components/doc-list-filters";
import { BureauSnapshot } from "@/components/bureau-snapshot";

// Read wiki/ filesystem at request time, not build time.
export const dynamic = "force-dynamic";

export default async function Home() {
  const ids = await listDocuments();
  const locale = await getLocale();
  const summaryLang: "pt" | "en" = locale === "en" ? "en" : "pt";

  const docs = await Promise.all(
    ids.map(async (id) => {
      const f = await readDocument(id);
      return {
        id,
        title: (f?.fm.canonical_title as string) ?? id,
        pages: (f?.fm.page_count as number) ?? 0,
        collection: (f?.fm.collection as string) ?? "uncategorized",
        classification: (f?.fm.highest_classification as string) ?? "—",
        summary: pickPitch(f?.fm as Record<string, unknown> | undefined, summaryLang) ?? (f?.body ? summarize(f.body, summaryLang) : ""),
      };
    }),
  );

  return (
    <main className="min-h-screen p-6 md:p-10 max-w-[1600px] mx-auto">
      <header className="mb-12 border-b border-[rgba(0,255,156,0.32)] pb-6">
        <div className="flex items-start justify-between gap-4 mb-2">
          <div className="font-mono text-[10px] text-[#5a6678] tracking-[0.18em] uppercase">
            // THE DISCLOSURE BUREAU // CLASSIFIED ARCHIVE //
          </div>
          <div className="flex items-center gap-2">
            <Link
              href="/search"
              className="font-mono text-xs px-3 py-1.5 border border-[#ffa500] text-[#ffa500] hover:bg-[rgba(255,165,0,0.10)] rounded"
              title="Hybrid search (or ⌘K anywhere)"
            >
              🔍 search
            </Link>
            <Link
              href="/timeline"
              className="font-mono text-xs px-3 py-1.5 border border-[#ff8a4d] text-[#ff8a4d] hover:bg-[rgba(255,138,77,0.10)] rounded"
            >
              📅 timeline
            </Link>
            <Link
              href="/graph"
              className="font-mono text-xs px-3 py-1.5 border border-[#7fdbff] text-[#7fdbff] hover:bg-[rgba(127,219,255,0.10)] rounded"
            >
              🕸 graph
            </Link>
            <Link
              href="/admin/stats"
              className="font-mono text-xs px-3 py-1.5 border border-[#a78bfa] text-[#a78bfa] hover:bg-[rgba(167,139,250,0.10)] rounded"
              title="Corpus analytics"
            >
              📊 stats
            </Link>
            <Link
              href="/bureau"
              className="font-mono text-xs px-3 py-1.5 border border-[#e0c080] text-[#e0c080] hover:bg-[rgba(224,192,128,0.10)] rounded"
              title="Investigation Bureau — detectives, hypotheses, evidence, contradictions, outliers, case reports"
            >
              🔎 bureau
            </Link>
            <Link
              href="/admin/batch"
              className="font-mono text-xs px-3 py-1.5 border border-[rgba(0,255,156,0.30)] text-[#00ff9c] hover:bg-[rgba(0,255,156,0.10)] rounded"
              title="Batch rebuild progress"
            >
              📈 batch
            </Link>
            <AuthBar />
          </div>
        </div>
        <h1 className="font-mono text-3xl text-[#00ff9c] mb-2">
          ▍ The Disclosure Bureau
        </h1>
        <p className="text-[#8896aa] text-sm">
          {docs.length} declassified documents · {docs.reduce((s, d) => s + d.pages, 0)} pages ·
          investigated by 8 AI detectives (Holmes · Locard · Dupin · Schneier · Poirot · Taleb · Tetlock · Case-Writer)
        </p>
      </header>

      <BureauSnapshot />

      <BatchProgressBanner />

      <DocListFilters docs={docs} />

      <ChatBubble context={{}} />
    </main>
  );
}
