import { NextResponse } from "next/server";
import { listDocuments, readDocument } from "@/lib/wiki";

export async function GET() {
  const ids = await listDocuments();
  const docs = await Promise.all(
    ids.map(async (id) => {
      const f = await readDocument(id);
      if (!f) return null;
      return {
        doc_id: id,
        canonical_title: f.fm.canonical_title ?? id,
        page_count: f.fm.page_count ?? 0,
        collection: f.fm.collection ?? "uncategorized",
        document_class: f.fm.document_class ?? "report",
        highest_classification: f.fm.highest_classification ?? "UNCLASSIFIED",
        content_classification: f.fm.content_classification ?? [],
        languages_detected: f.fm.languages_detected ?? [],
      };
    }),
  );
  return NextResponse.json({ documents: docs.filter(Boolean) });
}
