// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: io.atcr.hold.updateCrewTier
//
// This file was automatically generated from Lexicon schemas.
// Any manual changes will be overwritten on the next regeneration.

#[allow(unused_imports)]
use alloc::collections::BTreeMap;

#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::CowStr;
use jacquard_common::types::string::Did;
use jacquard_derive::{IntoStatic, lexicon, open_union};
use serde::{Serialize, Deserialize};

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct UpdateCrewTier<'a> {
    ///Tier rank index (0-based, maps to hold tier list by position).
    pub tier_rank: i64,
    ///DID of the crew member whose tier is being updated.
    #[serde(borrow)]
    pub user_did: Did<'a>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct UpdateCrewTierOutput<'a> {
    ///Resolved tier name on this hold.
    #[serde(borrow)]
    pub tier_name: CowStr<'a>,
}


#[open_union]
#[derive(
    Serialize,
    Deserialize,
    Debug,
    Clone,
    PartialEq,
    Eq,
    thiserror::Error,
    miette::Diagnostic,
    IntoStatic
)]

#[serde(tag = "error", content = "message")]
#[serde(bound(deserialize = "'de: 'a"))]
pub enum UpdateCrewTierError<'a> {
    /// Valid appview token required.
    #[serde(rename = "AuthRequired")]
    AuthRequired(Option<CowStr<'a>>),
    /// User is not a crew member on this hold.
    #[serde(rename = "UserNotFound")]
    UserNotFound(Option<CowStr<'a>>),
}

impl core::fmt::Display for UpdateCrewTierError<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::AuthRequired(msg) => {
                write!(f, "AuthRequired")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::UserNotFound(msg) => {
                write!(f, "UserNotFound")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::Unknown(err) => write!(f, "Unknown error: {:?}", err),
        }
    }
}

/// Response type for io.atcr.hold.updateCrewTier
pub struct UpdateCrewTierResponse;
impl jacquard_common::xrpc::XrpcResp for UpdateCrewTierResponse {
    const NSID: &'static str = "io.atcr.hold.updateCrewTier";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = UpdateCrewTierOutput<'de>;
    type Err<'de> = UpdateCrewTierError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for UpdateCrewTier<'a> {
    const NSID: &'static str = "io.atcr.hold.updateCrewTier";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = UpdateCrewTierResponse;
}

/// Endpoint type for io.atcr.hold.updateCrewTier
pub struct UpdateCrewTierRequest;
impl jacquard_common::xrpc::XrpcEndpoint for UpdateCrewTierRequest {
    const PATH: &'static str = "/xrpc/io.atcr.hold.updateCrewTier";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = UpdateCrewTier<'de>;
    type Response = UpdateCrewTierResponse;
}

pub mod update_crew_tier_state {

    pub use crate::builder_types::{Set, Unset, IsSet, IsUnset};
    #[allow(unused)]
    use ::core::marker::PhantomData;
    mod sealed {
        pub trait Sealed {}
    }
    /// State trait tracking which required fields have been set
    pub trait State: sealed::Sealed {
        type UserDid;
        type TierRank;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type UserDid = Unset;
        type TierRank = Unset;
    }
    ///State transition - sets the `user_did` field to Set
    pub struct SetUserDid<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetUserDid<S> {}
    impl<S: State> State for SetUserDid<S> {
        type UserDid = Set<members::user_did>;
        type TierRank = S::TierRank;
    }
    ///State transition - sets the `tier_rank` field to Set
    pub struct SetTierRank<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetTierRank<S> {}
    impl<S: State> State for SetTierRank<S> {
        type UserDid = S::UserDid;
        type TierRank = Set<members::tier_rank>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `user_did` field
        pub struct user_did(());
        ///Marker type for the `tier_rank` field
        pub struct tier_rank(());
    }
}

/// Builder for constructing an instance of this type
pub struct UpdateCrewTierBuilder<'a, S: update_crew_tier_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<i64>, Option<Did<'a>>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> UpdateCrewTier<'a> {
    /// Create a new builder for this type
    pub fn new() -> UpdateCrewTierBuilder<'a, update_crew_tier_state::Empty> {
        UpdateCrewTierBuilder::new()
    }
}

impl<'a> UpdateCrewTierBuilder<'a, update_crew_tier_state::Empty> {
    /// Create a new builder with all fields unset
    pub fn new() -> Self {
        UpdateCrewTierBuilder {
            _state: PhantomData,
            _fields: (None, None),
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> UpdateCrewTierBuilder<'a, S>
where
    S: update_crew_tier_state::State,
    S::TierRank: update_crew_tier_state::IsUnset,
{
    /// Set the `tierRank` field (required)
    pub fn tier_rank(
        mut self,
        value: impl Into<i64>,
    ) -> UpdateCrewTierBuilder<'a, update_crew_tier_state::SetTierRank<S>> {
        self._fields.0 = Option::Some(value.into());
        UpdateCrewTierBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> UpdateCrewTierBuilder<'a, S>
where
    S: update_crew_tier_state::State,
    S::UserDid: update_crew_tier_state::IsUnset,
{
    /// Set the `userDid` field (required)
    pub fn user_did(
        mut self,
        value: impl Into<Did<'a>>,
    ) -> UpdateCrewTierBuilder<'a, update_crew_tier_state::SetUserDid<S>> {
        self._fields.1 = Option::Some(value.into());
        UpdateCrewTierBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> UpdateCrewTierBuilder<'a, S>
where
    S: update_crew_tier_state::State,
    S::UserDid: update_crew_tier_state::IsSet,
    S::TierRank: update_crew_tier_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> UpdateCrewTier<'a> {
        UpdateCrewTier {
            tier_rank: self._fields.0.unwrap(),
            user_did: self._fields.1.unwrap(),
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> UpdateCrewTier<'a> {
        UpdateCrewTier {
            tier_rank: self._fields.0.unwrap(),
            user_did: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}