/**
 * System status page types and service catalog — system-status-page.
 *
 * STATUS_SERVICES is the canonical ordered list of services shown on the
 * public status page. IDs are snake_case and must match the values stored in
 * system_incidents.affected_services TEXT[].
 */

export const STATUS_SERVICES = [
  { id: 'app',              label: 'App (app.zync.is)' },
  { id: 'api',              label: 'API' },
  { id: 'file_uploads',     label: 'File uploads (R2)' },
  { id: 'email_delivery',   label: 'Email delivery' },
  { id: 'payments',         label: 'Payment processing' },
  { id: 'ai_assistant',     label: 'AI assistant' },
  { id: 'customer_portals', label: 'Customer portals' },
] as const

export type StatusServiceId = (typeof STATUS_SERVICES)[number]['id']

/** Per-service health */
export type ServiceStatus = 'operational' | 'degraded' | 'outage'

/** Lifecycle status of an incident (matches CHECK constraint in schema) */
export type IncidentStatus =
  | 'investigating'
  | 'identified'
  | 'monitoring'
  | 'resolved'

/** Severity of an incident (matches CHECK constraint in schema) */
export type IncidentImpact = 'minor' | 'major' | 'critical'

// ── DTOs (serialized for public API and admin UI) ─────────────────────────────

export interface IncidentUpdateObject {
  id: string
  body: string
  status: IncidentStatus
  createdAt: string  // ISO 8601
}

export interface IncidentObject {
  id: string
  title: string
  status: IncidentStatus
  impact: IncidentImpact
  affectedServices: StatusServiceId[]
  createdAt: string   // ISO 8601
  resolvedAt: string | null
  updates: IncidentUpdateObject[]
}

export interface ServiceStatusObject {
  id: StatusServiceId
  label: string
  status: ServiceStatus
}

/** Public status payload — cached in STATUS_KV with 30s TTL */
export interface StatusPayload {
  overall: ServiceStatus
  services: ServiceStatusObject[]
  activeIncidents: IncidentObject[]
  updatedAt: string  // ISO 8601
}

/** One calendar day entry for the uptime bar chart */
export interface StatusHistoryDay {
  date: string         // YYYY-MM-DD
  status: ServiceStatus  // worst status that day
}

/** History response for the past N days */
export interface StatusHistoryPayload {
  days: StatusHistoryDay[]
  uptimePct: number     // e.g. 99.8 — rounded to 1 dp
  incidents: IncidentObject[]
}
