import { apiRoute } from '@/lib/api/handler'
import { ApiError, ERROR_CODES } from '@/lib/api/errors'
import { getConnector } from '@/lib/services/integration-service'

export const dynamic = 'force-dynamic'

/** Health and quality history for one connector. */
export const GET = apiRoute<{ code: string }>(
  { permission: 'integrations.read' },
  async ({ params }) => {
    const connector = await getConnector(params.code)
    if (!connector) throw new ApiError(ERROR_CODES.notFound)
    return {
      code: connector.code,
      mode: connector.mode,
      status: connector.status,
      label: connector.label,
      healthScore: connector.healthScore,
      dataQuality: connector.dataQuality,
      latencyMs: connector.latencyMs,
      errorCount: connector.errorCount,
      eventsPerMin: connector.eventsPerMin,
      faultKind: connector.faultKind,
      samples: connector.samples,
    }
  },
)
