import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
import { errorDashboardService } from '@/lib/services/error-dashboard-service'

// GET /api/v1/admin/enterprise-clients/[id]/error-dashboard
// Get aggregated error dashboard data for a client
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const session = await auth()
  if (!session || session.user.userType !== 'staff') {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  const { id: clientId } = await params

  // Verify client exists
  const client = await prisma.enterpriseClient.findUnique({
    where: { id: clientId },
  })

  if (!client) {
    return NextResponse.json({ error: 'Client not found' }, { status: 404 })
  }

  try {
    const dashboard = await errorDashboardService.getClientDashboard(clientId)
    return NextResponse.json(dashboard)
  } catch (error) {
    console.error('Failed to get error dashboard:', error)
    return NextResponse.json(
      { error: 'Failed to get error dashboard' },
      { status: 500 }
    )
  }
}
