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 }> };

async function proxy(request: NextRequest, { params }: Params) {
  const { id } = await params;
  const body = request.method === 'PATCH' ? await request.text() : undefined;
  const response = await fetch(`${API}/users/${id}`, {
    method: request.method,
    headers: { Cookie: request.headers.get('cookie') ?? '', ...(body ? { 'Content-Type': 'application/json' } : {}) },
    ...(body ? { body } : {}),
    cache: 'no-store',
  });
  const data = await response.json().catch(() => ({}));
  return NextResponse.json(data, { status: response.status });
}

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