package com.correx.apps.server.health

import com.correx.core.events.events.HealthSubject
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable

/** Current health of a monitored subject (observability-spec §4). */
enum class HealthStatus { HEALTHY, DEGRADED }

/**
 * Read-model accumulated by [HealthProjection] — a pure fold over the system session's
 * health events. Holds the latest known status per subject; rebuilt from events on demand,
 * never persisted (Hard Invariant #1). A subject absent from [subjects] has never reported,
 * which the render layer treats as healthy ("no degradation recorded").
 */
@Serializable
data class HealthState(
    val subjects: Map<HealthSubject, SubjectHealth> = emptyMap(),
) {
    fun with(subject: HealthSubject, health: SubjectHealth): HealthState =
        copy(subjects = subjects + (subject to health))
}

@Serializable
data class SubjectHealth(
    val status: HealthStatus,
    val metric: String,
    val observedValue: Long,
    val detail: String,
    /** Timestamp of the edge event that put the subject into [status]. */
    val since: Instant,
)
