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

/**
 * Content API Proxy: Job 상태 조회
 * GET /api/content/images/jobs/{jobId}
 */
export async function GET(
  request: NextRequest,
  { params }: { params: { jobId: string } }
) {
  try {
    const { jobId } = params;

    const CONTENT_API_HOST = 'http://kt-event-marketing-api.20.214.196.128.nip.io';
    const backendUrl = `${CONTENT_API_HOST}/api/v1/content/images/jobs/${jobId}`;

    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,
      jobStatus: data.status,
      progress: data.progress,
    });

    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 }
    );
  }
}
