import { apiRoute } from '@/lib/api/handler'
import { crossValidate, getDataHealth } from '@/lib/services/data-health-service'

export const dynamic = 'force-dynamic'

/**
 * Instrumentation health and cross-sensor corroboration (§78, §79, §80).
 *
 * The headline counts are over the whole fleet even when the row list is bounded: a page
 * that silently counted only the worst forty sensors would be describing a different grid
 * from the one it names.
 */
export const GET = apiRoute({ permission: 'datahealth.read' }, async ({ searchParams }) => {
  const limit = Math.min(200, Math.max(1, Number(searchParams.get('limit')) || 40))
  const assetCode = searchParams.get('asset')

  if (assetCode) {
    const validation = await crossValidate({ assetCode })
    return { assetCode, ...validation }
  }

  return getDataHealth({ limit })
})
