import { renderTokenLogoSvg } from "@pons-agent/protocol";
import { getTokenAsset } from "@/lib/token-assets-db";

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

export async function GET(
  _request: Request,
  context: { params: Promise<{ assetId: string }> },
) {
  const { assetId } = await context.params;
  if (!/^[a-f0-9]{64}$/.test(assetId)) return new Response("Not found", { status: 404 });
  const record = getTokenAsset(assetId);
  if (!record) return new Response("Not found", { status: 404 });

  const body = record.content
    ? new Uint8Array(record.content)
    : renderTokenLogoSvg(record.asset);
  const contentType = record.content ? "image/png" : "image/svg+xml; charset=utf-8";

  return new Response(body, {
    headers: {
      "content-type": contentType,
      "cache-control": "public, max-age=31536000, immutable",
      "content-security-policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox",
      "x-content-type-options": "nosniff",
    },
  });
}
