import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import {
  getAdminPlatformAnalytics,
  getAdminConversationTrend,
  getAdminCallLatencyTrend,
  getAdminErrorBreakdown,
} 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 [analytics, conversationTrend, latencyTrend, errorBreakdown] = await Promise.all([
      getAdminPlatformAnalytics(),
      getAdminConversationTrend(),
      getAdminCallLatencyTrend(),
      getAdminErrorBreakdown(),
    ]);
    return NextResponse.json({ analytics, conversationTrend, latencyTrend, errorBreakdown });
  })
);
