//! Stdout / tracing log channel. Emits each ticket as a single
//! `tracing::warn!` event with structured fields. The simplest adapter;
//! useful for dev + non-prod environments where humans tail the agent
//! process logs.

use super::{ChannelAdapter, ChannelError};
use crate::escalation::trait_::EscalationTicket;
use async_trait::async_trait;

/// Stdout/tracing channel adapter.
pub struct LogChannel;

impl LogChannel {
    /// Build a new log channel.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for LogChannel {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl ChannelAdapter for LogChannel {
    async fn deliver(&self, ticket: &EscalationTicket) -> Result<(), ChannelError> {
        tracing::warn!(
            tenant = ?ticket.tenant,
            severity = ?ticket.severity,
            reason = %ticket.reason,
            provenance_run_id = ?ticket.provenance.as_ref().map(|p| &p.run_id),
            "escalation ticket"
        );
        Ok(())
    }

    fn name(&self) -> &'static str {
        "LogChannel"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::escalation::trait_::{EscalationTicket, Severity};

    #[tokio::test]
    async fn log_channel_delivers_ok() {
        let ch = LogChannel::new();
        let ticket = EscalationTicket {
            tenant: None,
            severity: Severity::High,
            reason: "test".into(),
            provenance: None,
        };
        ch.deliver(&ticket).await.expect("log channel delivers");
    }
}
