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

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