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

const DISTRIBUTION_API_BASE_URL = process.env.NEXT_PUBLIC_DISTRIBUTION_HOST || 'http://kt-event-marketing-api.20.214.196.128.nip.io';

export async function GET(
  request: NextRequest,
  { params }: { params: { eventId: string } }
) {
  try {
    const { eventId } = params;

    console.log('🔄 Proxying distribution status request to Distribution API:', {
      url: `${DISTRIBUTION_API_BASE_URL}/api/v1/distribution/${eventId}/status`,
      eventId,
    });

    const response = await fetch(`${DISTRIBUTION_API_BASE_URL}/distribution/${eventId}/status`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });

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

    const data = await response.json();
    console.log('✅ Distribution status 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 }
    );
  }
}
