import { NextResponse } from "next/server";
import { detectLocalMindmapFiles } from "@/lib/server/mindmap-files";
import type { MediaAsset } from "@/types/media";
import path from "path";
import { promises as fs } from "fs";
import { isConvexEnabled } from "@/lib/convex/enabled";
import { getAuthedConvexClient } from "@/lib/convex/route";
import { api } from "@/lib/convex/api";
import { maybeProxyForBrowserUrl } from "@/lib/url/proxyForBrowser";
import { HttpError } from "@/lib/auth/authContext";

export const dynamic = "force-dynamic";

type AgentAssetItem = {
  kind: "media" | "local-mindmap" | "test-pdf";
  id: string;
  title: string;
  fileUrl: string;
  mimeType?: string | null;
  assetType?: string | null;
  fileName?: string | null;
};

const TEST_DIR = path.join(process.cwd(), "test");

async function listTestPdfs(): Promise<AgentAssetItem[]> {
  try {
    const entries = await fs.readdir(TEST_DIR);
    return entries
      .filter((name) => name.toLowerCase().endsWith(".pdf"))
      .slice(0, 200)
      .map((name) => ({
        kind: "test-pdf" as const,
        id: `test-pdf:${name}`,
        title: name,
        fileUrl: `/api/mindmap-ai/test-pdf?name=${encodeURIComponent(name)}`,
        mimeType: "application/pdf",
        assetType: "file",
        fileName: name,
      }));
  } catch {
    return [];
  }
}

export async function GET(request: Request) {
  if (!isConvexEnabled()) {
    return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
  }

  const { searchParams } = new URL(request.url);
  const documentId = String(searchParams.get("documentId") ?? "").trim();
  const q = String(searchParams.get("q") ?? "").trim().toLowerCase();
  const workspaceOnly = String(searchParams.get("workspaceOnly") ?? "").trim() === "1";
  if (!documentId) {
    return NextResponse.json({ error: "缺少 documentId" }, { status: 400 });
  }

  let auth: { userId: string };
  let client: any;
  try {
    const res = await getAuthedConvexClient();
    auth = res.auth;
    client = res.client;
  } catch (err) {
    if (err instanceof HttpError) {
      return NextResponse.json({ error: "未登录" }, { status: err.status });
    }
    return NextResponse.json({ error: "未登录" }, { status: 401 });
  }
  const doc = await client.query(api.documents.getMeta, { id: documentId });
  if (!doc) {
    return NextResponse.json({ error: "页面不存在" }, { status: 404 });
  }

  if (workspaceOnly) {
    return NextResponse.json({
      ok: true,
      workspaceId: (doc as any).workspace_id ?? null,
      documentId,
      items: [],
    });
  }

  // 说明：这里的附件列表主要用于 Mindmap AI Agent 选择引用素材。
  const mediaRows = (await client.query(api.mediaAssets.listByDocument, {
    userId: auth.userId,
    documentId,
    limit: 200,
  })) as unknown as MediaAsset[];

  const mediaAssets: AgentAssetItem[] = (mediaRows ?? []).map((row) => ({
    kind: "media" as const,
    id: String((row as any).id ?? ""),
    title: String((row as any).file_name ?? (row as any).id ?? "附件"),
    fileUrl: maybeProxyForBrowserUrl(request, String((row as any).file_url ?? "")),
    mimeType: (row as any).mime_type ?? null,
    assetType: (row as any).asset_type ?? null,
    fileName: (row as any).file_name ?? null,
  }));

  const localMindmaps = (await detectLocalMindmapFiles([documentId]))
    .filter((x) => x.documentId === documentId)
    .map((x) => ({
      kind: "local-mindmap" as const,
      id: `mindmap:${x.mindmapId}`,
      title: x.fileName,
      fileUrl: `/documents/${documentId}/${x.fileName}`,
      mimeType: "application/json",
      assetType: "mindmap",
      fileName: x.fileName,
    }));

  const testPdfs = await listTestPdfs();

  let items: AgentAssetItem[] = [...localMindmaps, ...mediaAssets, ...testPdfs];
  if (q) {
    items = items.filter((it) => {
      const hay = `${it.title} ${it.fileName ?? ""}`.toLowerCase();
      return hay.includes(q);
    });
  }

  return NextResponse.json({
    ok: true,
    workspaceId: (doc as any).workspace_id ?? null,
    documentId,
    items,
  });

  /*
  const supabase = await createSupabaseRouteClient();
  const {
    data: { session },
  } = await supabase.auth.getSession();
  if (!session) {
    return NextResponse.json({ error: "未登录" }, { status: 401 });
  }

  const { searchParams } = new URL(request.url);
  const documentId = String(searchParams.get("documentId") ?? "").trim();
  const q = String(searchParams.get("q") ?? "").trim().toLowerCase();
  if (!documentId) {
    return NextResponse.json({ error: "缺少 documentId" }, { status: 400 });
  }

  const { data: doc, error: docError } = await supabase
    .from("documents")
    .select("id,workspace_id,title")
    .eq("id", documentId)
    .eq("user_id", session.user.id)
    .maybeSingle();
  if (docError) {
    return NextResponse.json({ error: docError.message }, { status: 400 });
  }
  if (!doc) {
    return NextResponse.json({ error: "页面不存在" }, { status: 404 });
  }

  const { data: mediaRows } = await supabase
    .from("media_assets")
    .select("id,asset_type,file_url,file_name,mime_type,document_id,workspace_id,deleted_at")
    .eq("document_id", documentId)
    .eq("workspace_id", doc.workspace_id)
    .is("deleted_at", null)
    .order("created_at", { ascending: false })
    .limit(200);

  const mediaAssets = ((mediaRows ?? []) as MediaAsset[]).map((row) => ({
    kind: "media" as const,
    id: String(row.id),
    title: String(row.file_name ?? row.id ?? "附件"),
    fileUrl: String(row.file_url ?? ""),
    mimeType: (row as any).mime_type ?? null,
    assetType: (row as any).asset_type ?? null,
    fileName: (row as any).file_name ?? null,
  }));

  const localMindmaps = (await detectLocalMindmapFiles([documentId]))
    .filter((x) => x.documentId === documentId)
    .map((x) => ({
      kind: "local-mindmap" as const,
      id: `mindmap:${x.mindmapId}`,
      title: x.fileName,
      fileUrl: `/documents/${documentId}/${x.fileName}`,
      mimeType: "application/json",
      assetType: "mindmap",
      fileName: x.fileName,
    }));

  const testPdfs = await listTestPdfs();

  let items: AgentAssetItem[] = [...localMindmaps, ...mediaAssets, ...testPdfs];
  if (q) {
    items = items.filter((it) => {
      const hay = `${it.title} ${it.fileName ?? ""}`.toLowerCase();
      return hay.includes(q);
    });
  }

  return NextResponse.json({
    ok: true,
    workspaceId: doc.workspace_id,
    documentId,
    items,
  });
  */
}
