import { NextRequest, NextResponse } from 'next/server';
import { GetAvatarPageQuery } from '@/lib/page-queries/media/GetAvatarPageQuery';

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ driverId: string }> }
) {
  const { driverId } = await params;
  
  const result = await GetAvatarPageQuery.execute({ driverId });
  
  if (result.isErr()) {
    const error = result.getError();
    if (error === 'notFound') {
      return new NextResponse(null, { status: 404 });
    }
    return new NextResponse(null, { status: 500 });
  }
  
  const viewData = result.unwrap();

  return new NextResponse(Buffer.from(viewData.buffer, 'base64'), {
    headers: {
      'Content-Type': viewData.contentType,
      'Cache-Control': 'public, max-age=3600',
    },
  });
}