import { NextRequest, NextResponse } from 'next/server';

export async function GET(
  request: NextRequest,
  { params }: { params: { driverId: string } }
) {
  const { driverId } = params;
  
  // In test environment, proxy to the mock API
  const apiBaseUrl = process.env.API_BASE_URL || 'http://localhost:3000';
  
  try {
    const response = await fetch(`${apiBaseUrl}/media/avatar/${driverId}`, {
      method: 'GET',
      headers: {
        'Content-Type': 'image/png',
      },
    });

    if (!response.ok) {
      // Return a fallback image or 404
      return new NextResponse(null, { status: 404 });
    }

    const buffer = await response.arrayBuffer();
    
    return new NextResponse(buffer, {
      headers: {
        'Content-Type': 'image/png',
        'Cache-Control': 'public, max-age=3600',
      },
    });
  } catch (error) {
    console.error('Error fetching avatar:', error);
    return new NextResponse(null, { status: 500 });
  }
}