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

/**
 * Content API Proxy: 이미지 생성
 * POST /api/content/images/generate
 */
export async function POST(request: NextRequest) {
  try {
    const body = await request.json();

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

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

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

    const data = await backendResponse.json();

    console.log('✅ Content API Response:', {
      status: backendResponse.status,
      data,
    });

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