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

const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
type Params = { params: Promise<{ id: string; addressId: string }> };

async function proxy(request: NextRequest, { params }: Params) {
  const { id, addressId } = await params;
  const response = await fetch(`${API}/users/${id}/addresses/${addressId}`, {
    method: request.method,
    headers: { Cookie: request.headers.get('cookie') ?? '' },
    cache: 'no-store',
  });
  if (response.status === 204) return new NextResponse(null, { status: 204 });
  const data = await response.json().catch(() => ({}));
  return NextResponse.json(data, { status: response.status });
}

export async function PATCH(request: NextRequest, context: Params) { return proxy(request, context); }
export async function DELETE(request: NextRequest, context: Params) { return proxy(request, context); }
