import { apiRoute } from '@/lib/api/handler'
import { ApiError, ERROR_CODES } from '@/lib/api/errors'
import { demoBaseTimestamp } from '@/lib/demo/config'
import { inspectGridModel } from '@/lib/services/physics-grid-service'

export const dynamic = 'force-dynamic'

/**
 * What is wrong with the electrical model, and how badly the seeded ratings are calibrated.
 *
 * Reports only. Nothing on this path corrects a value — §2 is explicit that a model must
 * not be repaired without the change being recorded, and the honest expression of that is
 * a validator that has no write path at all.
 */
export const GET = apiRoute({ permission: 'twin.read', limit: 60 }, async ({ searchParams }) => {
  const report = await inspectGridModel({
    twinCode: searchParams.get('twin') ?? undefined,
    ts: searchParams.get('clock') === 'demo' ? demoBaseTimestamp() : undefined,
  })
  if (!report) throw new ApiError(ERROR_CODES.notFound)
  return report
})
