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

/**
 * Content API Proxy: 이미지 조회
 * GET /api/content/events/{eventId}/images
 */
export async function GET(
  request: NextRequest,
  { params }: { params: { eventId: string } }
) {
  try {
    const { eventId } = params;
    const { searchParams } = new URL(request.url);

    const CONTENT_API_HOST = 'http://kt-event-marketing-api.20.214.196.128.nip.io';
    const queryString = searchParams.toString();
    const backendUrl = `${CONTENT_API_HOST}/api/v1/content/events/${eventId}/images${queryString ? `?${queryString}` : ''}`;

    console.log('🎨 Proxying to Content API:', backendUrl);

    const backendResponse = await fetch(backendUrl, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': request.headers.get('Authorization') || '',
      },
    });

    const data = await backendResponse.json();

    console.log('✅ Content API Response:', {
      status: backendResponse.status,
      imageCount: Array.isArray(data) ? data.length : 0,
    });

    return NextResponse.json(data, { status: backendResponse.status });
  } catch (error) {
    console.error('❌ Content API Proxy Error:', error);

    return NextResponse.json(
      {
        success: false,
        errorCode: 'PROXY_ERROR',
        message: 'Content API 호출 중 오류가 발생했습니다',
        details: error instanceof Error ? error.message : 'Unknown error',
      },
      { status: 500 }
    );
  }
}
