import { readFile } from "node:fs/promises";
import { NextRequest, NextResponse } from "next/server";
import { resolveUploadPath } from "@/app/lib/uploads";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";

export async function GET(
  _request: NextRequest,
  context: { params: Promise<{ id: string }> },
) {
  const { id } = await context.params;
  const absolutePath = await resolveUploadPath(id);

  if (!absolutePath) {
    return new NextResponse("Not found", { status: 404 });
  }

  const bytes = await readFile(absolutePath);
  return new NextResponse(bytes, {
    headers: {
      "Content-Type": "image/webp",
      "Cache-Control": "public, max-age=31536000, immutable",
      "X-Content-Type-Options": "nosniff",
    },
  });
}
