// @generated — terminal surface mapping for `MeerkatMachine`
// Generated by `xtask protocol-codegen`
// Exhaustive match — adding a new TurnTerminalOutcome or TurnTerminalCauseKind variant forces a compile-time update.

use crate::turn_execution_authority::{TurnTerminalCauseKind, TurnTerminalOutcome};

/// Surface result classification for turn execution terminal outcomes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SurfaceResultClass {
    Success,
    HardFailure,
    Cancelled,
    MissingTerminal,
}

/// Normalized terminal cause class for surface classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TerminalCauseClass {
    Missing,
    Unknown,
    BudgetExhausted,
    TimeBudgetExceeded,
    RetryExhausted,
    StructuredOutputValidationFailed,
    OtherFailure,
}

fn classify_cause(cause_kind: Option<TurnTerminalCauseKind>) -> TerminalCauseClass {
    match cause_kind {
        None => TerminalCauseClass::Missing,
        Some(TurnTerminalCauseKind::Unknown) => TerminalCauseClass::Unknown,
        Some(TurnTerminalCauseKind::BudgetExhausted) => TerminalCauseClass::BudgetExhausted,
        Some(TurnTerminalCauseKind::TimeBudgetExceeded) => TerminalCauseClass::TimeBudgetExceeded,
        Some(TurnTerminalCauseKind::RetryExhausted) => TerminalCauseClass::RetryExhausted,
        Some(TurnTerminalCauseKind::StructuredOutputValidationFailed) => {
            TerminalCauseClass::StructuredOutputValidationFailed
        }
        Some(
            TurnTerminalCauseKind::HookDenied
            | TurnTerminalCauseKind::HookFailure
            | TurnTerminalCauseKind::LlmFailure
            | TurnTerminalCauseKind::ToolFailure
            | TurnTerminalCauseKind::TurnLimitReached
            | TurnTerminalCauseKind::RuntimeApplyFailure
            | TurnTerminalCauseKind::FatalFailure,
        ) => TerminalCauseClass::OtherFailure,
    }
}

/// Exhaustive terminal outcome/cause classification for `MeerkatMachine`.
/// No default arm — adding a new `TurnTerminalOutcome` or `TurnTerminalCauseKind` variant forces a compile-time update.
pub fn classify_terminal(
    outcome: &TurnTerminalOutcome,
    cause_kind: Option<TurnTerminalCauseKind>,
) -> SurfaceResultClass {
    match (*outcome, classify_cause(cause_kind)) {
        (TurnTerminalOutcome::None, TerminalCauseClass::Missing) => {
            SurfaceResultClass::MissingTerminal
        }
        (TurnTerminalOutcome::None, TerminalCauseClass::Unknown) => {
            SurfaceResultClass::MissingTerminal
        }
        (TurnTerminalOutcome::None, TerminalCauseClass::BudgetExhausted) => {
            SurfaceResultClass::MissingTerminal
        }
        (TurnTerminalOutcome::None, TerminalCauseClass::TimeBudgetExceeded) => {
            SurfaceResultClass::MissingTerminal
        }
        (TurnTerminalOutcome::None, TerminalCauseClass::RetryExhausted) => {
            SurfaceResultClass::MissingTerminal
        }
        (TurnTerminalOutcome::None, TerminalCauseClass::StructuredOutputValidationFailed) => {
            SurfaceResultClass::MissingTerminal
        }
        (TurnTerminalOutcome::None, TerminalCauseClass::OtherFailure) => {
            SurfaceResultClass::MissingTerminal
        }
        (TurnTerminalOutcome::Completed, TerminalCauseClass::Missing) => {
            SurfaceResultClass::Success
        }
        (TurnTerminalOutcome::Completed, TerminalCauseClass::Unknown) => {
            SurfaceResultClass::Success
        }
        (TurnTerminalOutcome::Completed, TerminalCauseClass::BudgetExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Completed, TerminalCauseClass::TimeBudgetExceeded) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Completed, TerminalCauseClass::RetryExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Completed, TerminalCauseClass::StructuredOutputValidationFailed) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Completed, TerminalCauseClass::OtherFailure) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Failed, TerminalCauseClass::Missing) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Failed, TerminalCauseClass::Unknown) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Failed, TerminalCauseClass::BudgetExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Failed, TerminalCauseClass::TimeBudgetExceeded) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Failed, TerminalCauseClass::RetryExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Failed, TerminalCauseClass::StructuredOutputValidationFailed) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Failed, TerminalCauseClass::OtherFailure) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Cancelled, TerminalCauseClass::Missing) => {
            SurfaceResultClass::Cancelled
        }
        (TurnTerminalOutcome::Cancelled, TerminalCauseClass::Unknown) => {
            SurfaceResultClass::Cancelled
        }
        (TurnTerminalOutcome::Cancelled, TerminalCauseClass::BudgetExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Cancelled, TerminalCauseClass::TimeBudgetExceeded) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Cancelled, TerminalCauseClass::RetryExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Cancelled, TerminalCauseClass::StructuredOutputValidationFailed) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::Cancelled, TerminalCauseClass::OtherFailure) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::BudgetExhausted, TerminalCauseClass::BudgetExhausted) => {
            SurfaceResultClass::Success
        }
        (TurnTerminalOutcome::BudgetExhausted, TerminalCauseClass::Missing) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::BudgetExhausted, TerminalCauseClass::Unknown) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::BudgetExhausted, TerminalCauseClass::TimeBudgetExceeded) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::BudgetExhausted, TerminalCauseClass::RetryExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (
            TurnTerminalOutcome::BudgetExhausted,
            TerminalCauseClass::StructuredOutputValidationFailed,
        ) => SurfaceResultClass::HardFailure,
        (TurnTerminalOutcome::BudgetExhausted, TerminalCauseClass::OtherFailure) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::TimeBudgetExceeded, TerminalCauseClass::Missing) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::TimeBudgetExceeded, TerminalCauseClass::Unknown) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::TimeBudgetExceeded, TerminalCauseClass::BudgetExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::TimeBudgetExceeded, TerminalCauseClass::TimeBudgetExceeded) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::TimeBudgetExceeded, TerminalCauseClass::RetryExhausted) => {
            SurfaceResultClass::HardFailure
        }
        (
            TurnTerminalOutcome::TimeBudgetExceeded,
            TerminalCauseClass::StructuredOutputValidationFailed,
        ) => SurfaceResultClass::HardFailure,
        (TurnTerminalOutcome::TimeBudgetExceeded, TerminalCauseClass::OtherFailure) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::StructuredOutputValidationFailed, TerminalCauseClass::Missing) => {
            SurfaceResultClass::HardFailure
        }
        (TurnTerminalOutcome::StructuredOutputValidationFailed, TerminalCauseClass::Unknown) => {
            SurfaceResultClass::HardFailure
        }
        (
            TurnTerminalOutcome::StructuredOutputValidationFailed,
            TerminalCauseClass::BudgetExhausted,
        ) => SurfaceResultClass::HardFailure,
        (
            TurnTerminalOutcome::StructuredOutputValidationFailed,
            TerminalCauseClass::TimeBudgetExceeded,
        ) => SurfaceResultClass::HardFailure,
        (
            TurnTerminalOutcome::StructuredOutputValidationFailed,
            TerminalCauseClass::RetryExhausted,
        ) => SurfaceResultClass::HardFailure,
        (
            TurnTerminalOutcome::StructuredOutputValidationFailed,
            TerminalCauseClass::StructuredOutputValidationFailed,
        ) => SurfaceResultClass::HardFailure,
        (
            TurnTerminalOutcome::StructuredOutputValidationFailed,
            TerminalCauseClass::OtherFailure,
        ) => SurfaceResultClass::HardFailure,
    }
}
