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

export const dynamic = 'force-dynamic';

const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3000';

/** Scan-meter proxy (Session 60, night2/F) — forwards the caller's auth to
 *  Express, which reads the live limiter's rolling-24h usage. */
export async function GET(req: NextRequest) {
  const auth = req.headers.get('authorization');
  if (!auth) return NextResponse.json({ unlimited: false, used: null, limit: null }, { status: 401 });
  try {
    const upstream = await fetch(`${BACKEND_URL}/api/user/scan-meter`, {
      headers: { Accept: 'application/json', Authorization: auth },
      cache: 'no-store',
    });
    const data = await upstream.json().catch(() => ({}));
    return NextResponse.json(data, { status: upstream.ok ? 200 : upstream.status });
  } catch {
    return NextResponse.json({ unlimited: false, used: null, limit: null }, { status: 200 });
  }
}
