// @generated — Generated by `cargo xtask machine-codegen --all`.
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::implicit_clone,
    clippy::unnecessary_cast,
    clippy::redundant_clone
)]

pub fn schema() -> meerkat_machine_schema::MachineSchema {
    meerkat_machine_schema::catalog::dsl::dsl_auth_machine()
}

#[allow(non_camel_case_types)]
#[derive(
    Debug,
    Clone,
    Copy,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub enum AuthLifecyclePhase {
    #[default]
    #[serde(rename = "Valid")]
    Valid,
    #[serde(rename = "Expiring")]
    Expiring,
    #[serde(rename = "Refreshing")]
    Refreshing,
    #[serde(rename = "ReauthRequired")]
    ReauthRequired,
    #[serde(rename = "Released")]
    Released,
}
impl AuthLifecyclePhase {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Valid => "Valid",
            Self::Expiring => "Expiring",
            Self::Refreshing => "Refreshing",
            Self::ReauthRequired => "ReauthRequired",
            Self::Released => "Released",
        }
    }
}
impl std::convert::TryFrom<&str> for AuthLifecyclePhase {
    type Error = String;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "Valid" => Ok(Self::Valid),
            "Expiring" => Ok(Self::Expiring),
            "Refreshing" => Ok(Self::Refreshing),
            "ReauthRequired" => Ok(Self::ReauthRequired),
            "Released" => Ok(Self::Released),
            other => Err(format!("invalid AuthLifecyclePhase value `{other}`")),
        }
    }
}
impl std::convert::TryFrom<String> for AuthLifecyclePhase {
    type Error = String;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::try_from(value.as_str())
    }
}
impl std::fmt::Display for AuthLifecyclePhase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

pub trait Context {}
pub struct EmptyContext;
impl Context for EmptyContext {}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Phase {
    Valid,
    Expiring,
    Refreshing,
    ReauthRequired,
    Released,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct State {
    pub phase: Phase,
    pub expires_at: Option<u64>,
    pub last_refresh: Option<u64>,
    pub refresh_attempt: u64,
    pub credential_present: bool,
    pub oauth_browser_flow_ids: std::collections::BTreeSet<String>,
    pub oauth_browser_flow_providers: std::collections::BTreeMap<String, String>,
    pub oauth_browser_flow_redirect_uris: std::collections::BTreeMap<String, String>,
    pub oauth_browser_flow_expires_at_millis: std::collections::BTreeMap<String, u64>,
    pub oauth_device_flow_ids: std::collections::BTreeSet<String>,
    pub oauth_device_flow_providers: std::collections::BTreeMap<String, String>,
    pub oauth_device_flow_expires_at_millis: std::collections::BTreeMap<String, u64>,
    pub oauth_device_poll_ids: std::collections::BTreeSet<String>,
    pub oauth_outstanding_flow_count: u64,
}
impl Default for State {
    fn default() -> Self {
        initial_state()
    }
}

pub mod inputs {
    #[allow(unused_imports)]
    use super::*;
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Acquire {
        pub expires_at_ts: Option<u64>,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct MarkExpiring {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct BeginRefresh {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct CompleteRefresh {
        pub new_expires_at: Option<u64>,
        pub now_ts: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct RefreshFailedTransient {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct RefreshFailedPermanent {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct MarkReauthRequired {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct ClearCredentialLifecycle {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Release {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct AdmitOAuthBrowserFlow {
        pub flow_id: String,
        pub provider: String,
        pub redirect_uri: String,
        pub expires_at_millis: u64,
        pub max_outstanding_flows: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct VerifyOAuthBrowserFlow {
        pub flow_id: String,
        pub provider: String,
        pub redirect_uri: String,
        pub now_millis: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct ConsumeOAuthBrowserFlow {
        pub flow_id: String,
        pub provider: String,
        pub redirect_uri: String,
        pub now_millis: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct ExpireOAuthBrowserFlow {
        pub flow_id: String,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct AdmitOAuthDeviceFlow {
        pub flow_id: String,
        pub provider: String,
        pub expires_at_millis: u64,
        pub max_outstanding_flows: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct VerifyOAuthDeviceFlow {
        pub flow_id: String,
        pub provider: String,
        pub now_millis: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct BeginOAuthDevicePoll {
        pub flow_id: String,
        pub provider: String,
        pub now_millis: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct FinishOAuthDevicePoll {
        pub flow_id: String,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct ConsumeOAuthDeviceFlow {
        pub flow_id: String,
        pub provider: String,
        pub now_millis: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct ExpireOAuthDeviceFlow {
        pub flow_id: String,
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Input {
    Acquire(inputs::Acquire),
    MarkExpiring(inputs::MarkExpiring),
    BeginRefresh(inputs::BeginRefresh),
    CompleteRefresh(inputs::CompleteRefresh),
    RefreshFailedTransient(inputs::RefreshFailedTransient),
    RefreshFailedPermanent(inputs::RefreshFailedPermanent),
    MarkReauthRequired(inputs::MarkReauthRequired),
    ClearCredentialLifecycle(inputs::ClearCredentialLifecycle),
    Release(inputs::Release),
    AdmitOAuthBrowserFlow(inputs::AdmitOAuthBrowserFlow),
    VerifyOAuthBrowserFlow(inputs::VerifyOAuthBrowserFlow),
    ConsumeOAuthBrowserFlow(inputs::ConsumeOAuthBrowserFlow),
    ExpireOAuthBrowserFlow(inputs::ExpireOAuthBrowserFlow),
    AdmitOAuthDeviceFlow(inputs::AdmitOAuthDeviceFlow),
    VerifyOAuthDeviceFlow(inputs::VerifyOAuthDeviceFlow),
    BeginOAuthDevicePoll(inputs::BeginOAuthDevicePoll),
    FinishOAuthDevicePoll(inputs::FinishOAuthDevicePoll),
    ConsumeOAuthDeviceFlow(inputs::ConsumeOAuthDeviceFlow),
    ExpireOAuthDeviceFlow(inputs::ExpireOAuthDeviceFlow),
}
impl Input {
    pub fn kind(&self) -> InputKind {
        match self {
            Self::Acquire(_) => InputKind::Acquire,
            Self::MarkExpiring(_) => InputKind::MarkExpiring,
            Self::BeginRefresh(_) => InputKind::BeginRefresh,
            Self::CompleteRefresh(_) => InputKind::CompleteRefresh,
            Self::RefreshFailedTransient(_) => InputKind::RefreshFailedTransient,
            Self::RefreshFailedPermanent(_) => InputKind::RefreshFailedPermanent,
            Self::MarkReauthRequired(_) => InputKind::MarkReauthRequired,
            Self::ClearCredentialLifecycle(_) => InputKind::ClearCredentialLifecycle,
            Self::Release(_) => InputKind::Release,
            Self::AdmitOAuthBrowserFlow(_) => InputKind::AdmitOAuthBrowserFlow,
            Self::VerifyOAuthBrowserFlow(_) => InputKind::VerifyOAuthBrowserFlow,
            Self::ConsumeOAuthBrowserFlow(_) => InputKind::ConsumeOAuthBrowserFlow,
            Self::ExpireOAuthBrowserFlow(_) => InputKind::ExpireOAuthBrowserFlow,
            Self::AdmitOAuthDeviceFlow(_) => InputKind::AdmitOAuthDeviceFlow,
            Self::VerifyOAuthDeviceFlow(_) => InputKind::VerifyOAuthDeviceFlow,
            Self::BeginOAuthDevicePoll(_) => InputKind::BeginOAuthDevicePoll,
            Self::FinishOAuthDevicePoll(_) => InputKind::FinishOAuthDevicePoll,
            Self::ConsumeOAuthDeviceFlow(_) => InputKind::ConsumeOAuthDeviceFlow,
            Self::ExpireOAuthDeviceFlow(_) => InputKind::ExpireOAuthDeviceFlow,
        }
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum InputKind {
    Acquire,
    MarkExpiring,
    BeginRefresh,
    CompleteRefresh,
    RefreshFailedTransient,
    RefreshFailedPermanent,
    MarkReauthRequired,
    ClearCredentialLifecycle,
    Release,
    AdmitOAuthBrowserFlow,
    VerifyOAuthBrowserFlow,
    ConsumeOAuthBrowserFlow,
    ExpireOAuthBrowserFlow,
    AdmitOAuthDeviceFlow,
    VerifyOAuthDeviceFlow,
    BeginOAuthDevicePoll,
    FinishOAuthDevicePoll,
    ConsumeOAuthDeviceFlow,
    ExpireOAuthDeviceFlow,
}

pub mod effects {
    #[allow(unused_imports)]
    use super::*;
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct EmitLifecycleEvent {
        pub new_state: AuthLifecyclePhase,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct WakeRefreshLoop {}
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Effect {
    EmitLifecycleEvent(effects::EmitLifecycleEvent),
    WakeRefreshLoop(effects::WakeRefreshLoop),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum EffectKind {
    EmitLifecycleEvent,
    WakeRefreshLoop,
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TransitionId {
    Acquire,
    MarkExpiring,
    BeginRefreshFromValid,
    BeginRefreshFromExpiring,
    CompleteRefresh,
    RefreshFailedTransient,
    RefreshFailedPermanent,
    MarkReauthRequiredFromValid,
    MarkReauthRequiredFromExpiring,
    MarkReauthRequiredFromRefreshing,
    ClearCredentialLifecycle,
    Release,
    AdmitOAuthBrowserFlowValid,
    AdmitOAuthBrowserFlowExpiring,
    AdmitOAuthBrowserFlowRefreshing,
    AdmitOAuthBrowserFlowReauthRequired,
    VerifyOAuthBrowserFlowValid,
    VerifyOAuthBrowserFlowExpiring,
    VerifyOAuthBrowserFlowRefreshing,
    VerifyOAuthBrowserFlowReauthRequired,
    ConsumeOAuthBrowserFlowValid,
    ConsumeOAuthBrowserFlowExpiring,
    ConsumeOAuthBrowserFlowRefreshing,
    ConsumeOAuthBrowserFlowReauthRequired,
    ExpireOAuthBrowserFlowValid,
    ExpireOAuthBrowserFlowExpiring,
    ExpireOAuthBrowserFlowRefreshing,
    ExpireOAuthBrowserFlowReauthRequired,
    AdmitOAuthDeviceFlowValid,
    AdmitOAuthDeviceFlowExpiring,
    AdmitOAuthDeviceFlowRefreshing,
    AdmitOAuthDeviceFlowReauthRequired,
    VerifyOAuthDeviceFlowValid,
    VerifyOAuthDeviceFlowExpiring,
    VerifyOAuthDeviceFlowRefreshing,
    VerifyOAuthDeviceFlowReauthRequired,
    BeginOAuthDevicePollValid,
    BeginOAuthDevicePollExpiring,
    BeginOAuthDevicePollRefreshing,
    BeginOAuthDevicePollReauthRequired,
    FinishOAuthDevicePollValid,
    FinishOAuthDevicePollExpiring,
    FinishOAuthDevicePollRefreshing,
    FinishOAuthDevicePollReauthRequired,
    ConsumeOAuthDeviceFlowValid,
    ConsumeOAuthDeviceFlowExpiring,
    ConsumeOAuthDeviceFlowRefreshing,
    ConsumeOAuthDeviceFlowReauthRequired,
    ExpireOAuthDeviceFlowValid,
    ExpireOAuthDeviceFlowExpiring,
    ExpireOAuthDeviceFlowRefreshing,
    ExpireOAuthDeviceFlowReauthRequired,
}
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum GuardId {
    None,
}
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum HelperId {
    None,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct GuardRejection {
    pub transition_id: TransitionId,
    pub guard_id: GuardId,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TriggerDiscriminant {
    Input(InputKind),
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TransitionRefusal {
    NoMatchingTransition {
        phase: Phase,
        trigger: TriggerDiscriminant,
    },
    GuardRejected {
        rejections: Vec<GuardRejection>,
    },
    AmbiguousTransition {
        transitions: Vec<TransitionId>,
    },
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum KernelError {
    ContextViolation {
        transition_id: TransitionId,
        detail: String,
    },
    HelperEvaluation {
        helper_id: HelperId,
        detail: String,
    },
    CodegenInvariant {
        detail: String,
    },
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TransitionError {
    Refusal(TransitionRefusal),
    Kernel(KernelError),
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Outcome {
    pub transition_id: TransitionId,
    pub next_state: State,
    pub effects: Vec<Effect>,
}

pub mod helpers {
    #[allow(unused_imports)]
    use super::*;
    pub fn none<C: Context>(_: &State, context: &C) -> Result<(), KernelError> {
        let _ = context;
        Ok(())
    }
}

pub fn initial_state() -> State {
    State {
        phase: Phase::Valid,
        expires_at: None,
        last_refresh: None,
        refresh_attempt: 0,
        credential_present: false,
        oauth_browser_flow_ids: Default::default(),
        oauth_browser_flow_providers: Default::default(),
        oauth_browser_flow_redirect_uris: Default::default(),
        oauth_browser_flow_expires_at_millis: Default::default(),
        oauth_device_flow_ids: Default::default(),
        oauth_device_flow_providers: Default::default(),
        oauth_device_flow_expires_at_millis: Default::default(),
        oauth_device_poll_ids: Default::default(),
        oauth_outstanding_flow_count: 0,
    }
}
