// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: tools.ozone.team.updateMember
//
// 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};
use crate::tools_ozone::team::Member;

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct UpdateMember<'a> {
    #[serde(borrow)]
    pub did: Did<'a>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disabled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub role: Option<UpdateMemberRole<'a>>,
}


#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum UpdateMemberRole<'a> {
    RoleAdmin,
    RoleModerator,
    RoleVerifier,
    RoleTriage,
    Other(CowStr<'a>),
}

impl<'a> UpdateMemberRole<'a> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::RoleAdmin => "tools.ozone.team.defs#roleAdmin",
            Self::RoleModerator => "tools.ozone.team.defs#roleModerator",
            Self::RoleVerifier => "tools.ozone.team.defs#roleVerifier",
            Self::RoleTriage => "tools.ozone.team.defs#roleTriage",
            Self::Other(s) => s.as_ref(),
        }
    }
}

impl<'a> From<&'a str> for UpdateMemberRole<'a> {
    fn from(s: &'a str) -> Self {
        match s {
            "tools.ozone.team.defs#roleAdmin" => Self::RoleAdmin,
            "tools.ozone.team.defs#roleModerator" => Self::RoleModerator,
            "tools.ozone.team.defs#roleVerifier" => Self::RoleVerifier,
            "tools.ozone.team.defs#roleTriage" => Self::RoleTriage,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> From<String> for UpdateMemberRole<'a> {
    fn from(s: String) -> Self {
        match s.as_str() {
            "tools.ozone.team.defs#roleAdmin" => Self::RoleAdmin,
            "tools.ozone.team.defs#roleModerator" => Self::RoleModerator,
            "tools.ozone.team.defs#roleVerifier" => Self::RoleVerifier,
            "tools.ozone.team.defs#roleTriage" => Self::RoleTriage,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> core::fmt::Display for UpdateMemberRole<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<'a> AsRef<str> for UpdateMemberRole<'a> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<'a> serde::Serialize for UpdateMemberRole<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de, 'a> serde::Deserialize<'de> for UpdateMemberRole<'a>
where
    'de: 'a,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = <&'de str>::deserialize(deserializer)?;
        Ok(Self::from(s))
    }
}

impl<'a> Default for UpdateMemberRole<'a> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl jacquard_common::IntoStatic for UpdateMemberRole<'_> {
    type Output = UpdateMemberRole<'static>;
    fn into_static(self) -> Self::Output {
        match self {
            UpdateMemberRole::RoleAdmin => UpdateMemberRole::RoleAdmin,
            UpdateMemberRole::RoleModerator => UpdateMemberRole::RoleModerator,
            UpdateMemberRole::RoleVerifier => UpdateMemberRole::RoleVerifier,
            UpdateMemberRole::RoleTriage => UpdateMemberRole::RoleTriage,
            UpdateMemberRole::Other(v) => UpdateMemberRole::Other(v.into_static()),
        }
    }
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct UpdateMemberOutput<'a> {
    #[serde(flatten)]
    #[serde(borrow)]
    pub value: Member<'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 UpdateMemberError<'a> {
    /// The member being updated does not exist in the team
    #[serde(rename = "MemberNotFound")]
    MemberNotFound(Option<CowStr<'a>>),
}

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

/// Response type for tools.ozone.team.updateMember
pub struct UpdateMemberResponse;
impl jacquard_common::xrpc::XrpcResp for UpdateMemberResponse {
    const NSID: &'static str = "tools.ozone.team.updateMember";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = UpdateMemberOutput<'de>;
    type Err<'de> = UpdateMemberError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for UpdateMember<'a> {
    const NSID: &'static str = "tools.ozone.team.updateMember";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = UpdateMemberResponse;
}

/// Endpoint type for tools.ozone.team.updateMember
pub struct UpdateMemberRequest;
impl jacquard_common::xrpc::XrpcEndpoint for UpdateMemberRequest {
    const PATH: &'static str = "/xrpc/tools.ozone.team.updateMember";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = UpdateMember<'de>;
    type Response = UpdateMemberResponse;
}

pub mod update_member_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 Did;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Did = Unset;
    }
    ///State transition - sets the `did` field to Set
    pub struct SetDid<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetDid<S> {}
    impl<S: State> State for SetDid<S> {
        type Did = Set<members::did>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `did` field
        pub struct did(());
    }
}

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

impl<'a> UpdateMember<'a> {
    /// Create a new builder for this type
    pub fn new() -> UpdateMemberBuilder<'a, update_member_state::Empty> {
        UpdateMemberBuilder::new()
    }
}

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

impl<'a, S> UpdateMemberBuilder<'a, S>
where
    S: update_member_state::State,
    S::Did: update_member_state::IsUnset,
{
    /// Set the `did` field (required)
    pub fn did(
        mut self,
        value: impl Into<Did<'a>>,
    ) -> UpdateMemberBuilder<'a, update_member_state::SetDid<S>> {
        self._fields.0 = Option::Some(value.into());
        UpdateMemberBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: update_member_state::State> UpdateMemberBuilder<'a, S> {
    /// Set the `disabled` field (optional)
    pub fn disabled(mut self, value: impl Into<Option<bool>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `disabled` field to an Option value (optional)
    pub fn maybe_disabled(mut self, value: Option<bool>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S: update_member_state::State> UpdateMemberBuilder<'a, S> {
    /// Set the `role` field (optional)
    pub fn role(mut self, value: impl Into<Option<UpdateMemberRole<'a>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `role` field to an Option value (optional)
    pub fn maybe_role(mut self, value: Option<UpdateMemberRole<'a>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<'a, S> UpdateMemberBuilder<'a, S>
where
    S: update_member_state::State,
    S::Did: update_member_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> UpdateMember<'a> {
        UpdateMember {
            did: self._fields.0.unwrap(),
            disabled: self._fields.1,
            role: self._fields.2,
            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>,
        >,
    ) -> UpdateMember<'a> {
        UpdateMember {
            did: self._fields.0.unwrap(),
            disabled: self._fields.1,
            role: self._fields.2,
            extra_data: Some(extra_data),
        }
    }
}