import { NextRequest, NextResponse } from 'next/server';
import { logError } from '@/lib/logger';
import { getCurrentUserFromRequest } from '@/lib/auth-server';
import * as billingClient from '@/lib/billing-client';
export async function GET(req: NextRequest) {
  try {
    const caller = await getCurrentUserFromRequest(req);
    if (!caller) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }
    const url = new URL(req.url);
    const days = parseInt(url.searchParams.get('days') ?? '30', 10);
    const userId = url.searchParams.get('userId') ?? undefined;
    const productId = url.searchParams.get('productId') ?? undefined;
    const summary = url.searchParams.get('summary') === 'true';
    if (summary) {
      const result = await billingClient.getUsageSummary(days, userId, productId);
      return NextResponse.json(result);
    }
    const result = await billingClient.listUsage({ userId, days, productId });
    return NextResponse.json(result);
  } catch (error) {
    logError('Usage error', error);
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
  }
}
