import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import {
  getAdminDashboardStats,
  getAdminMrrHistory,
  getAdminRecentRestaurants,
} 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 [stats, mrrHistory, recentRestaurants] = await Promise.all([
      getAdminDashboardStats(),
      getAdminMrrHistory(),
      getAdminRecentRestaurants(),
    ]);
    return NextResponse.json({ stats, mrrHistory, recentRestaurants });
  })
);
