import { NextResponse } from "next/server";
import { readFile } from "fs/promises";
import path from "path";
import { debugDirPath } from "@/lib/threads-browser/debug";

export async function GET(
  _request: Request,
  { params }: { params: Promise<{ id: string; file: string }> }
) {
  try {
    const { id, file } = await params;
    if (id.includes("..") || file.includes("..") || !file.endsWith(".png")) {
      return NextResponse.json({ error: "無效的檔案" }, { status: 400 });
    }

    const bytes = await readFile(path.join(debugDirPath(), id, file));
    return new NextResponse(bytes, {
      headers: {
        "Content-Type": "image/png",
        "Cache-Control": "private, max-age=3600",
      },
    });
  } catch {
    return NextResponse.json({ error: "找不到截圖" }, { status: 404 });
  }
}