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

const GATEWAY_HOST = 'http://kt-event-marketing-api.20.214.196.128.nip.io';

export async function GET(
  request: NextRequest,
  { params }: { params: { eventId: string; participantId: string } }
) {
  try {
    const { eventId, participantId } = params;
    const token = request.headers.get('Authorization');

    console.log('👤 [Proxy] Get participant request:', {
      eventId,
      participantId,
      hasToken: !!token,
      timestamp: new Date().toISOString(),
    });

    const headers: HeadersInit = {
      'Content-Type': 'application/json',
    };

    if (token) {
      headers['Authorization'] = token;
    }

    const response = await fetch(
      `${GATEWAY_HOST}/api/v1/participations/events/${eventId}/participants/${participantId}`,
      {
        method: 'GET',
        headers,
      }
    );

    const data = await response.json();

    console.log('📥 [Proxy] Get participant response:', {
      status: response.status,
      success: response.ok,
    });

    if (!response.ok) {
      return NextResponse.json(data, { status: response.status });
    }

    return NextResponse.json(data);
  } catch (error) {
    console.error('❌ [Proxy] Get participant error:', error);
    return NextResponse.json(
      { message: '참여자 조회 중 오류가 발생했습니다.' },
      { status: 500 }
    );
  }
}
