/**
 * Enum-like domain vocabularies.
 *
 * The Prisma schema stores these as plain strings so a single schema stays portable
 * across PostgreSQL and SQLite. Type-safety and validation live here instead and are
 * enforced with zod at every API boundary (src/lib/api/schemas.ts).
 */

export const GRID_STATES = ['normal', 'watch', 'warning', 'critical', 'emergency'] as const
export type GridState = (typeof GRID_STATES)[number]

export const ALERT_LEVELS = ['information', 'warning', 'high', 'critical', 'emergency'] as const
export type AlertLevel = (typeof ALERT_LEVELS)[number]

export const ALERT_STATUSES = ['new', 'acknowledged', 'in_progress', 'resolved', 'closed'] as const
export type AlertStatus = (typeof ALERT_STATUSES)[number]

export const SEVERITIES = ['low', 'medium', 'high', 'critical'] as const
export type Severity = (typeof SEVERITIES)[number]

export const ASSET_STATUSES = ['online', 'degraded', 'maintenance', 'offline'] as const
export type AssetStatus = (typeof ASSET_STATUSES)[number]

export const CRITICALITIES = ['low', 'medium', 'high', 'critical'] as const
export type Criticality = (typeof CRITICALITIES)[number]

export const ASSET_TYPE_KEYS = [
  'power_plant',
  'substation',
  'transformer',
  'transmission_line',
  'battery_storage',
  'solar_farm',
  'wind_farm',
  'industrial_load',
  'ev_hub',
] as const
export type AssetTypeKey = (typeof ASSET_TYPE_KEYS)[number]

export const SENSOR_KINDS = [
  'voltage',
  'current',
  'frequency',
  'temperature',
  'load',
  'power_factor',
  'thd',
] as const
export type SensorKind = (typeof SENSOR_KINDS)[number]

export const EVENT_TYPES = [
  'transformer_overload',
  'thermal_runaway',
  'voltage_collapse',
  'frequency_deviation',
  'line_overload',
  'insulation_failure',
  'battery_derate',
  'renewable_ramp_shortfall',
  'cascading_outage',
] as const
export type EventType = (typeof EVENT_TYPES)[number]

export const ACTION_TYPES = [
  'load_transfer',
  'battery_discharge',
  'demand_response',
  'cooling_boost',
  'network_reconfigure',
  'generation_redispatch',
  'maintenance_dispatch',
] as const
export type ActionType = (typeof ACTION_TYPES)[number]

export const RECOMMENDATION_STATUSES = [
  'proposed',
  'simulated',
  'approved',
  'executed',
  'rejected',
] as const
export type RecommendationStatus = (typeof RECOMMENDATION_STATUSES)[number]

export const SIMULATION_KINDS = ['what_if', 'preventive_action', 'demo', 'baseline'] as const
export type SimulationKind = (typeof SIMULATION_KINDS)[number]

export const PREDICTION_STATUSES = ['active', 'prevented', 'expired', 'realised'] as const
export type PredictionStatus = (typeof PREDICTION_STATUSES)[number]

export const MAINTENANCE_KINDS = ['preventive', 'corrective', 'predictive', 'inspection'] as const
export type MaintenanceKind = (typeof MAINTENANCE_KINDS)[number]

export const MAINTENANCE_STATUSES = ['scheduled', 'in_progress', 'completed', 'overdue'] as const
export type MaintenanceStatus = (typeof MAINTENANCE_STATUSES)[number]

export const ROLE_KEYS = [
  'system_admin',
  'executive',
  'control_operator',
  'engineer',
  'maintenance_engineer',
  'analyst',
  'viewer',
] as const
export type RoleKey = (typeof ROLE_KEYS)[number]

export const LOCALES = ['ar', 'en'] as const
export type Locale = (typeof LOCALES)[number]

export const NOTIFICATION_CHANNELS = ['dashboard', 'email', 'sms', 'webhook', 'push'] as const
export type NotificationChannel = (typeof NOTIFICATION_CHANNELS)[number]

export const INTEGRATION_STATUSES = ['connected', 'available', 'planned'] as const
export type IntegrationStatus = (typeof INTEGRATION_STATUSES)[number]

/** Risk score → grid state. The single source of truth for status colouring. */
export function riskToState(risk: number): GridState {
  if (risk >= 90) return 'emergency'
  if (risk >= 75) return 'critical'
  if (risk >= 55) return 'warning'
  if (risk >= 35) return 'watch'
  return 'normal'
}

/** Risk score → alert level, mirroring the escalation ladder in §16. */
export function riskToAlertLevel(risk: number): AlertLevel {
  if (risk >= 95) return 'emergency'
  if (risk >= 85) return 'critical'
  if (risk >= 70) return 'high'
  if (risk >= 50) return 'warning'
  return 'information'
}

export function riskToSeverity(risk: number): Severity {
  if (risk >= 85) return 'critical'
  if (risk >= 70) return 'high'
  if (risk >= 45) return 'medium'
  return 'low'
}

/** Health score → a coarse condition band used by Predictive Maintenance. */
export function healthToBand(health: number): 'excellent' | 'good' | 'fair' | 'poor' | 'critical' {
  if (health >= 90) return 'excellent'
  if (health >= 75) return 'good'
  if (health >= 60) return 'fair'
  if (health >= 45) return 'poor'
  return 'critical'
}
