import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/lib/auth'
import { statsCollectionService } from '@/lib/services/stats-collection-service'
import type { StatsRange } from '@/lib/services/stats-collection-service'

interface RouteContext {
  params: Promise<{ id: string }>
}

/**
 * GET /api/v1/admin/enterprise-clients/[id]/stats
 * Get aggregated stats overview for an enterprise client
 */
export async function GET(
  request: NextRequest,
  context: RouteContext
) {
  const session = await auth()
  if (!session || session.user.userType !== 'staff') {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  const { id: clientId } = await context.params
  const searchParams = request.nextUrl.searchParams
  const range = (searchParams.get('range') || '24h') as StatsRange

  try {
    const overview = await statsCollectionService.getClientStatsOverview(clientId)
    return NextResponse.json({
      ...overview,
      range
    })
  } catch (error) {
    console.error('Failed to get client stats:', error)
    return NextResponse.json(
      { error: 'Failed to get client stats' },
      { status: 500 }
    )
  }
}
