// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: io.atcr.hold.requestCrew
//
// 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, BosStr, DefaultStr, FromStaticStr};
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::string::{AtUri, Cid};
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct RequestCrew<S: BosStr = DefaultStr> {
    ///Requested permissions (default: ['blob:read', 'blob:write'])
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permissions: Option<Vec<S>>,
    ///Requested role (default: 'member')  Defaults to `"member"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "_default_request_crew_role")]
    pub role: Option<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct RequestCrewOutput<S: BosStr = DefaultStr> {
    ///CID of the crew record
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    ///Human-readable status message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<S>,
    ///Result status
    pub status: RequestCrewOutputStatus<S>,
    ///AT-URI of the crew record (if created or already exists)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<AtUri<S>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// Result status

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RequestCrewOutputStatus<S: BosStr = DefaultStr> {
    Created,
    AlreadyMember,
    Other(S),
}

impl<S: BosStr> RequestCrewOutputStatus<S> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Created => "created",
            Self::AlreadyMember => "already_member",
            Self::Other(s) => s.as_ref(),
        }
    }
    /// Construct from a string-like value, matching known values.
    pub fn from_value(s: S) -> Self {
        match s.as_ref() {
            "created" => Self::Created,
            "already_member" => Self::AlreadyMember,
            _ => Self::Other(s),
        }
    }
}

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

impl<S: BosStr> AsRef<str> for RequestCrewOutputStatus<S> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

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

impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for RequestCrewOutputStatus<S> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = S::deserialize(deserializer)?;
        Ok(Self::from_value(s))
    }
}

impl<S: BosStr + Default> Default for RequestCrewOutputStatus<S> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl<S: BosStr> jacquard_common::IntoStatic for RequestCrewOutputStatus<S>
where
    S: BosStr + jacquard_common::IntoStatic,
    S::Output: BosStr,
{
    type Output = RequestCrewOutputStatus<S::Output>;
    fn into_static(self) -> Self::Output {
        match self {
            RequestCrewOutputStatus::Created => RequestCrewOutputStatus::Created,
            RequestCrewOutputStatus::AlreadyMember => {
                RequestCrewOutputStatus::AlreadyMember
            }
            RequestCrewOutputStatus::Other(v) => {
                RequestCrewOutputStatus::Other(v.into_static())
            }
        }
    }
}


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

#[serde(tag = "error", content = "message")]
pub enum RequestCrewError {
    #[serde(rename = "AuthRequired")]
    AuthRequired(Option<SmolStr>),
    #[serde(rename = "RegistrationDisabled")]
    RegistrationDisabled(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for RequestCrewError {
    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::RegistrationDisabled(msg) => {
                write!(f, "RegistrationDisabled")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::Other { error, message } => {
                write!(f, "{}", error)?;
                if let Some(msg) = message {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
        }
    }
}

/** Response marker for the `io.atcr.hold.requestCrew` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `RequestCrewOutput<S>` for this endpoint.*/
pub struct RequestCrewResponse;
impl jacquard_common::xrpc::XrpcResp for RequestCrewResponse {
    const NSID: &'static str = "io.atcr.hold.requestCrew";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = RequestCrewOutput<S>;
    type Err = RequestCrewError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for RequestCrew<S> {
    const NSID: &'static str = "io.atcr.hold.requestCrew";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = RequestCrewResponse;
}

/** Endpoint marker for the `io.atcr.hold.requestCrew` procedure.

Path: `/xrpc/io.atcr.hold.requestCrew`. The request payload type is `RequestCrew<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
pub struct RequestCrewRequest;
impl jacquard_common::xrpc::XrpcEndpoint for RequestCrewRequest {
    const PATH: &'static str = "/xrpc/io.atcr.hold.requestCrew";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<S: BosStr> = RequestCrew<S>;
    type Response = RequestCrewResponse;
}

fn _default_request_crew_role<S: FromStaticStr>() -> ::core::option::Option<S> {
    Some(S::from_static("member"))
}