// @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_occurrence_lifecycle_machine()
}

#[derive(
    Debug,
    Clone,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub struct ClaimToken(pub String);
impl From<String> for ClaimToken {
    fn from(value: String) -> Self {
        Self(value)
    }
}
impl From<&str> for ClaimToken {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}
impl std::fmt::Display for ClaimToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}
#[derive(
    Debug,
    Clone,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub struct DeliveryReceipt(pub String);
impl From<String> for DeliveryReceipt {
    fn from(value: String) -> Self {
        Self(value)
    }
}
impl From<&str> for DeliveryReceipt {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}
impl std::fmt::Display for DeliveryReceipt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}
#[allow(non_camel_case_types)]
#[derive(
    Debug,
    Clone,
    Copy,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub enum OccurrenceFailureClass {
    #[default]
    #[serde(rename = "TargetMaterializationFailed")]
    TargetMaterializationFailed,
    #[serde(rename = "TargetMissing")]
    TargetMissing,
    #[serde(rename = "TargetBusy")]
    TargetBusy,
    #[serde(rename = "RuntimeRejected")]
    RuntimeRejected,
    #[serde(rename = "MobRejected")]
    MobRejected,
    #[serde(rename = "LeaseLost")]
    LeaseLost,
    #[serde(rename = "TransportError")]
    TransportError,
    #[serde(rename = "InternalError")]
    InternalError,
}
impl OccurrenceFailureClass {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::TargetMaterializationFailed => "TargetMaterializationFailed",
            Self::TargetMissing => "TargetMissing",
            Self::TargetBusy => "TargetBusy",
            Self::RuntimeRejected => "RuntimeRejected",
            Self::MobRejected => "MobRejected",
            Self::LeaseLost => "LeaseLost",
            Self::TransportError => "TransportError",
            Self::InternalError => "InternalError",
        }
    }
}
impl std::convert::TryFrom<&str> for OccurrenceFailureClass {
    type Error = String;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "TargetMaterializationFailed" => Ok(Self::TargetMaterializationFailed),
            "TargetMissing" => Ok(Self::TargetMissing),
            "TargetBusy" => Ok(Self::TargetBusy),
            "RuntimeRejected" => Ok(Self::RuntimeRejected),
            "MobRejected" => Ok(Self::MobRejected),
            "LeaseLost" => Ok(Self::LeaseLost),
            "TransportError" => Ok(Self::TransportError),
            "InternalError" => Ok(Self::InternalError),
            other => Err(format!("invalid OccurrenceFailureClass value `{other}`")),
        }
    }
}
impl std::convert::TryFrom<String> for OccurrenceFailureClass {
    type Error = String;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::try_from(value.as_str())
    }
}
impl std::fmt::Display for OccurrenceFailureClass {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}
#[derive(
    Debug,
    Clone,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub struct OccurrenceId(pub String);
impl From<String> for OccurrenceId {
    fn from(value: String) -> Self {
        Self(value)
    }
}
impl From<&str> for OccurrenceId {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}
impl std::fmt::Display for OccurrenceId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}
#[allow(non_camel_case_types)]
#[derive(
    Debug,
    Clone,
    Copy,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub enum OccurrenceLifecycleState {
    #[default]
    #[serde(rename = "Pending")]
    Pending,
    #[serde(rename = "Claimed")]
    Claimed,
    #[serde(rename = "Dispatching")]
    Dispatching,
    #[serde(rename = "AwaitingCompletion")]
    AwaitingCompletion,
    #[serde(rename = "Completed")]
    Completed,
    #[serde(rename = "Skipped")]
    Skipped,
    #[serde(rename = "Misfired")]
    Misfired,
    #[serde(rename = "Superseded")]
    Superseded,
    #[serde(rename = "DeliveryFailed")]
    DeliveryFailed,
}
impl OccurrenceLifecycleState {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Pending => "Pending",
            Self::Claimed => "Claimed",
            Self::Dispatching => "Dispatching",
            Self::AwaitingCompletion => "AwaitingCompletion",
            Self::Completed => "Completed",
            Self::Skipped => "Skipped",
            Self::Misfired => "Misfired",
            Self::Superseded => "Superseded",
            Self::DeliveryFailed => "DeliveryFailed",
        }
    }
}
impl std::convert::TryFrom<&str> for OccurrenceLifecycleState {
    type Error = String;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "Pending" => Ok(Self::Pending),
            "Claimed" => Ok(Self::Claimed),
            "Dispatching" => Ok(Self::Dispatching),
            "AwaitingCompletion" => Ok(Self::AwaitingCompletion),
            "Completed" => Ok(Self::Completed),
            "Skipped" => Ok(Self::Skipped),
            "Misfired" => Ok(Self::Misfired),
            "Superseded" => Ok(Self::Superseded),
            "DeliveryFailed" => Ok(Self::DeliveryFailed),
            other => Err(format!("invalid OccurrenceLifecycleState value `{other}`")),
        }
    }
}
impl std::convert::TryFrom<String> for OccurrenceLifecycleState {
    type Error = String;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::try_from(value.as_str())
    }
}
impl std::fmt::Display for OccurrenceLifecycleState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}
#[derive(
    Debug,
    Clone,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    serde::Serialize,
    serde::Deserialize,
)]
pub struct ScheduleId(pub String);
impl From<String> for ScheduleId {
    fn from(value: String) -> Self {
        Self(value)
    }
}
impl From<&str> for ScheduleId {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}
impl std::fmt::Display for ScheduleId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

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 {
    Pending,
    Claimed,
    Dispatching,
    AwaitingCompletion,
    Completed,
    Skipped,
    Misfired,
    Superseded,
    DeliveryFailed,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct State {
    pub phase: Phase,
    pub occurrence_id: OccurrenceId,
    pub schedule_id: ScheduleId,
    pub schedule_revision: u64,
    pub occurrence_ordinal: u64,
    pub target_binding_key: String,
    pub due_at_utc_ms: u64,
    pub claimed_by: Option<String>,
    pub lease_expires_at_utc_ms: Option<u64>,
    pub claimed_at_utc_ms: Option<u64>,
    pub claim_token: Option<ClaimToken>,
    pub delivery_correlation_id: Option<String>,
    pub last_receipt: Option<DeliveryReceipt>,
    pub failure_class: Option<OccurrenceFailureClass>,
    pub failure_detail: Option<String>,
    pub dispatched_at_utc_ms: Option<u64>,
    pub completed_at_utc_ms: Option<u64>,
    pub attempt_count: u64,
    pub superseded_by_revision: Option<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 Claim {
        pub owner_id: String,
        pub at_utc_ms: u64,
        pub lease_expires_at_utc_ms: u64,
        pub claim_token: ClaimToken,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct DispatchStarted {
        pub correlation_id: Option<String>,
        pub at_utc_ms: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct AwaitCompletion {
        pub at_utc_ms: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Complete {
        pub receipt: DeliveryReceipt,
        pub at_utc_ms: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Skip {
        pub detail: Option<String>,
        pub failure_class: Option<OccurrenceFailureClass>,
        pub at_utc_ms: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Misfire {
        pub detail: Option<String>,
        pub failure_class: Option<OccurrenceFailureClass>,
        pub at_utc_ms: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Supersede {
        pub superseded_by_revision: u64,
        pub at_utc_ms: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct DeliveryFailed {
        pub receipt: Option<DeliveryReceipt>,
        pub failure_class: OccurrenceFailureClass,
        pub detail: Option<String>,
        pub at_utc_ms: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct LeaseExpired {
        pub at_utc_ms: u64,
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Input {
    Claim(inputs::Claim),
    DispatchStarted(inputs::DispatchStarted),
    AwaitCompletion(inputs::AwaitCompletion),
    Complete(inputs::Complete),
    Skip(inputs::Skip),
    Misfire(inputs::Misfire),
    Supersede(inputs::Supersede),
    DeliveryFailed(inputs::DeliveryFailed),
    LeaseExpired(inputs::LeaseExpired),
}
impl Input {
    pub fn kind(&self) -> InputKind {
        match self {
            Self::Claim(_) => InputKind::Claim,
            Self::DispatchStarted(_) => InputKind::DispatchStarted,
            Self::AwaitCompletion(_) => InputKind::AwaitCompletion,
            Self::Complete(_) => InputKind::Complete,
            Self::Skip(_) => InputKind::Skip,
            Self::Misfire(_) => InputKind::Misfire,
            Self::Supersede(_) => InputKind::Supersede,
            Self::DeliveryFailed(_) => InputKind::DeliveryFailed,
            Self::LeaseExpired(_) => InputKind::LeaseExpired,
        }
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum InputKind {
    Claim,
    DispatchStarted,
    AwaitCompletion,
    Complete,
    Skip,
    Misfire,
    Supersede,
    DeliveryFailed,
    LeaseExpired,
}

pub mod effects {
    #[allow(unused_imports)]
    use super::*;
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Claimed {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct DispatchStarted {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct AwaitingCompletion {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Completed {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Skipped {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Misfired {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Superseded {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct OccurrencesSuperseded {
        pub occurrence_id: OccurrenceId,
        pub superseding_revision: u64,
    }
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct DeliveryFailed {}
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct LeaseExpired {}
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Effect {
    Claimed(effects::Claimed),
    DispatchStarted(effects::DispatchStarted),
    AwaitingCompletion(effects::AwaitingCompletion),
    Completed(effects::Completed),
    Skipped(effects::Skipped),
    Misfired(effects::Misfired),
    Superseded(effects::Superseded),
    OccurrencesSuperseded(effects::OccurrencesSuperseded),
    DeliveryFailed(effects::DeliveryFailed),
    LeaseExpired(effects::LeaseExpired),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum EffectKind {
    Claimed,
    DispatchStarted,
    AwaitingCompletion,
    Completed,
    Skipped,
    Misfired,
    Superseded,
    OccurrencesSuperseded,
    DeliveryFailed,
    LeaseExpired,
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TransitionId {
    ClaimPending,
    DispatchStartedFromClaimed,
    AwaitCompletionFromDispatching,
    CompleteFromDispatchingOrAwaiting,
    SkipFromPendingOrLive,
    MisfireFromPendingOrLive,
    SupersedePendingOrLive,
    DeliveryFailedFromClaimedOrLive,
    LeaseExpiredFromClaimed,
    LeaseExpiredFromDispatching,
    LeaseExpiredFromAwaitingCompletion,
}
#[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::Pending,
        occurrence_id: OccurrenceId::default(),
        schedule_id: ScheduleId::default(),
        schedule_revision: 0,
        occurrence_ordinal: 0,
        target_binding_key: String::new(),
        due_at_utc_ms: 0,
        claimed_by: None,
        lease_expires_at_utc_ms: None,
        claimed_at_utc_ms: None,
        claim_token: None,
        delivery_correlation_id: None,
        last_receipt: None,
        failure_class: None,
        failure_detail: None,
        dispatched_at_utc_ms: None,
        completed_at_utc_ms: None,
        attempt_count: 0,
        superseded_by_revision: None,
    }
}
