package no.svorka.mcms.ui

import androidx.compose.ui.graphics.Color
import no.svorka.mcms.core.AlarmSeverity
import no.svorka.mcms.core.ApiError
import no.svorka.mcms.core.OnuLifecycleState

/** Screen load state. */
sealed interface LoadPhase {
    data object Idle : LoadPhase
    data object Loading : LoadPhase
    data object Loaded : LoadPhase
    data class Failed(val message: String) : LoadPhase
}

fun OnuLifecycleState.color(): Color = when (this) {
    OnuLifecycleState.REGISTERED -> Color(0xFF2E7D32)
    OnuLifecycleState.UNSPECIFIED -> Color(0xFF26A69A)
    OnuLifecycleState.UNPROVISIONED -> Color(0xFF1565C0)
    OnuLifecycleState.DEREGISTERED -> Color(0xFF757575)
    OnuLifecycleState.DYING_GASP, OnuLifecycleState.DISALLOWED_ERROR -> Color(0xFFC62828)
    OnuLifecycleState.DISABLED, OnuLifecycleState.DISALLOWED_ADMIN, OnuLifecycleState.DISALLOWED_REG_ID -> Color(0xFFEF6C00)
    OnuLifecycleState.UNKNOWN -> Color(0xFF9E9E9E)
}

fun AlarmSeverity.color(): Color = when (this) {
    AlarmSeverity.EMERGENCY, AlarmSeverity.ALERT, AlarmSeverity.CRITICAL -> Color(0xFFC62828)
    AlarmSeverity.ERROR -> Color(0xFFEF6C00)
    AlarmSeverity.WARNING -> Color(0xFFF9A825)
    AlarmSeverity.NOTICE, AlarmSeverity.INFO -> Color(0xFF1565C0)
    AlarmSeverity.DEBUG -> Color(0xFF757575)
    AlarmSeverity.UNKNOWN -> Color(0xFF9E9E9E)
}

/**
 * Maps a thrown error to a user message. [ApiError] messages are written to be
 * user-facing; anything else is unexpected and could leak library internals, so
 * it collapses to a generic line.
 */
fun Throwable.uiMessage(): String = (this as? ApiError)?.message ?: "Something went wrong. Please try again."
