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

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

export async function PUT(request: NextRequest) {
  try {
    const token = request.headers.get('Authorization');
    const body = await request.json();

    console.log('🔑 [Proxy] Change password request:', {
      hasToken: !!token,
      timestamp: new Date().toISOString(),
    });

    if (!token) {
      return NextResponse.json(
        { message: '인증 토큰이 필요합니다.' },
        { status: 401 }
      );
    }

    const response = await fetch(`${GATEWAY_HOST}/api/v1/users/password`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        Authorization: token,
      },
      body: JSON.stringify(body),
    });

    if (!response.ok) {
      const data = await response.json();
      console.log('📥 [Proxy] Change password response:', {
        status: response.status,
        success: false,
      });
      return NextResponse.json(data, { status: response.status });
    }

    console.log('📥 [Proxy] Change password response:', {
      status: response.status,
      success: true,
    });

    return new NextResponse(null, { status: 200 });
  } catch (error) {
    console.error('❌ [Proxy] Change password error:', error);
    return NextResponse.json(
      { message: '비밀번호 변경 중 오류가 발생했습니다.' },
      { status: 500 }
    );
  }
}
