//! DTOs for escalation event management.

use serde::{Deserialize, Serialize};
use stemedb_core::types::{EscalationEvent, EscalationLevel};
use utoipa::ToSchema;

/// Escalation severity level (DTO).
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum EscalationLevelDto {
    /// Informational: flag for analytics, no action required.
    Low,
    /// Medium priority: flag for review queue.
    Medium,
    /// High priority: block dependent actions, require approval.
    High,
    /// Critical: immediate human intervention required.
    Critical,
}

impl From<EscalationLevel> for EscalationLevelDto {
    fn from(level: EscalationLevel) -> Self {
        match level {
            EscalationLevel::Low => EscalationLevelDto::Low,
            EscalationLevel::Medium => EscalationLevelDto::Medium,
            EscalationLevel::High => EscalationLevelDto::High,
            EscalationLevel::Critical => EscalationLevelDto::Critical,
        }
    }
}

impl From<EscalationLevelDto> for EscalationLevel {
    fn from(dto: EscalationLevelDto) -> Self {
        match dto {
            EscalationLevelDto::Low => EscalationLevel::Low,
            EscalationLevelDto::Medium => EscalationLevel::Medium,
            EscalationLevelDto::High => EscalationLevel::High,
            EscalationLevelDto::Critical => EscalationLevel::Critical,
        }
    }
}

/// Escalation event (DTO).
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct EscalationEventDto {
    /// Hex-encoded escalation ID.
    pub id: String,
    /// Subject of the conflicting assertion pair.
    pub subject: String,
    /// Predicate of the conflicting assertion pair.
    pub predicate: String,
    /// The conflict score that triggered this escalation (0.0 to 1.0).
    pub conflict_score: f32,
    /// Severity level assigned by the policy.
    pub level: EscalationLevelDto,
    /// Human-readable description of why this escalation was triggered.
    pub reason: String,
    /// Unix timestamp (nanoseconds) when this event was created.
    pub timestamp: u64,
    /// Has this escalation been acknowledged/resolved?
    pub resolved: bool,
}

impl From<&EscalationEvent> for EscalationEventDto {
    fn from(event: &EscalationEvent) -> Self {
        EscalationEventDto {
            id: hex::encode(event.id),
            subject: event.subject.clone(),
            predicate: event.predicate.clone(),
            conflict_score: event.conflict_score,
            level: event.level.into(),
            reason: event.reason.clone(),
            timestamp: event.timestamp,
            resolved: event.resolved,
        }
    }
}

/// Response for listing escalation events.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct EscalationListResponse {
    /// List of escalation events.
    pub escalations: Vec<EscalationEventDto>,
    /// Total count of escalations in this response.
    pub count: usize,
}
