import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import {
  getAdminBillingPlans,
  getAdminBillingUsage,
  getAdminInvoices,
} from '@server/services/admin.service';
import { ForbiddenError } from '@server/errors';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    if (req.session.role !== 'superadmin' && req.session.role !== 'support') {
      throw new ForbiddenError();
    }
    const [plans, usage, invoices] = await Promise.all([
      getAdminBillingPlans(),
      getAdminBillingUsage(),
      getAdminInvoices(),
    ]);
    return NextResponse.json({ plans, usage, invoices });
  })
);
