// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: tools.ozone.team.addMember
//
// 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 AddMember<'a> {
    #[serde(borrow)]
    pub did: Did<'a>,
    #[serde(borrow)]
    pub role: AddMemberRole<'a>,
}


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

impl<'a> AddMemberRole<'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 AddMemberRole<'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 AddMemberRole<'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 AddMemberRole<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

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

impl<'a> serde::Serialize for AddMemberRole<'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 AddMemberRole<'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 AddMemberRole<'a> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

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


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct AddMemberOutput<'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 AddMemberError<'a> {
    /// Member already exists in the team.
    #[serde(rename = "MemberAlreadyExists")]
    MemberAlreadyExists(Option<CowStr<'a>>),
}

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

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

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

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

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

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

impl<'a> AddMember<'a> {
    /// Create a new builder for this type
    pub fn new() -> AddMemberBuilder<'a, add_member_state::Empty> {
        AddMemberBuilder::new()
    }
}

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

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

impl<'a, S> AddMemberBuilder<'a, S>
where
    S: add_member_state::State,
    S::Role: add_member_state::IsUnset,
{
    /// Set the `role` field (required)
    pub fn role(
        mut self,
        value: impl Into<AddMemberRole<'a>>,
    ) -> AddMemberBuilder<'a, add_member_state::SetRole<S>> {
        self._fields.1 = Option::Some(value.into());
        AddMemberBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

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