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

const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';

/** GET /api/inventory/[variantId]/availability -> backend */
export async function GET(
  _req: NextRequest,
  ctx: { params: Promise<{ variantId: string }> },
) {
  const { variantId } = await ctx.params;
  try {
    const backendRes = await fetch(`${API}/inventory/${encodeURIComponent(variantId)}/availability`);
    const data = await backendRes.json().catch(() => null);
    return NextResponse.json(data ?? { error: 'Bad response' }, { status: backendRes.status });
  } catch {
    return NextResponse.json({ error: 'Proxy error' }, { status: 502 });
  }
}