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

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ leagueId: string }> }
) {
  const { leagueId } = await params;
  
  const result = await GetLeagueLogoPageQuery.execute({ leagueId });
  
  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',
    },
  });
}