import { NextRequest, NextResponse } from 'next/server';
import { loadImage } from '@/lib/image-service';

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ slug: string; id: string; assetId: string }> }
) {
  try {
    const { slug, id, assetId } = await params;
    const imageBuffer = await loadImage(slug, id, assetId);

    if (!imageBuffer) {
      return NextResponse.json({ error: 'Imagen no encontrada' }, { status: 404 });
    }

    return new NextResponse(new Uint8Array(imageBuffer), {
      headers: {
        'Content-Type': 'image/png',
        'Cache-Control': 'no-cache',
      },
    });
  } catch (error: any) {
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}
