import { NextResponse } from 'next/server';
import { readFile } from 'fs/promises';
import path from 'path';

export async function GET(request: Request, context: { params: Promise<{ name: string }> }) {
  try {
    const { name } = await context.params;
    const filePath = path.join(process.cwd(), 'uploads', 'products', name);
    const buffer = await readFile(filePath);
    const ext = name.split('.').pop()?.toLowerCase();
    const contentType = ext === 'png' ? 'image/png' : ext === 'webp' ? 'image/webp' : ext === 'gif' ? 'image/gif' : 'image/jpeg';
    return new NextResponse(buffer, { headers: { 'Content-Type': contentType, 'Cache-Control': 'public, max-age=86400' } });
  } catch {
    return NextResponse.json({ error: 'Bild nicht gefunden' }, { status: 404 });
  }
}
