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

const PARTICIPATION_API_BASE_URL = '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;

    console.log('🔄 Proxying get participant request to Participation API:', {
      url: `${PARTICIPATION_API_BASE_URL}/api/v1/participations/api/v1/events/${eventId}/participants/${participantId}`,
      eventId,
      participantId,
    });

    const response = await fetch(
      `${PARTICIPATION_API_BASE_URL}/api/v1/participations/api/v1/events/${eventId}/participants/${participantId}`,
      {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
        },
      }
    );

    if (!response.ok) {
      const errorText = await response.text();
      console.error('❌ Participation API error:', response.status, errorText);
      return NextResponse.json(
        { error: 'Failed to get participant', details: errorText },
        { status: response.status }
      );
    }

    const data = await response.json();
    console.log('✅ Participant retrieved:', data);

    return NextResponse.json(data);
  } catch (error) {
    console.error('❌ Proxy error:', error);
    return NextResponse.json(
      { error: 'Internal server error', details: error instanceof Error ? error.message : 'Unknown error' },
      { status: 500 }
    );
  }
}
