import { NextResponse } from 'next/server' import { db } from '@/lib/db' export const dynamic = 'force-dynamic' // GET /api/aos/agency // Returns all agency applications + approved agencies. Admin-only in practice // (no scope check here because AOS is single-tenant dev mode). export async function GET() { try { const [applications, agencies] = await Promise.all([ db.agencyApplication.findMany({ orderBy: { appliedAt: 'desc' }, include: { company: true }, }), db.company.findMany({ where: { isAgency: true, agencyStatus: 'approved' }, orderBy: { agencyApprovedAt: 'desc' }, include: { agencyClients: { select: { id: true, name: true, email: true } }, _count: { select: { agencyClients: true, licenses: true } }, }, }), ]) const pending = applications.filter((a) => a.status === 'pending') const reviewed = applications.filter((a) => a.status !== 'pending') return NextResponse.json({ applications: { pending, reviewed }, agencies, stats: { pending: pending.length, approved: agencies.length, totalApplications: applications.length, }, }) } catch (err) { const msg = err instanceof Error ? err.message : 'internal server error' return NextResponse.json({ error: msg }, { status: 500 }) } }