// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: chat.bsky.group.requestJoin
//
// 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::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::chat_bsky::convo::ConvoView;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct RequestJoin<S: BosStr = DefaultStr> {
    pub code: 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 RequestJoinOutput<S: BosStr = DefaultStr> {
    ///The group convo joined. This is only present in the case of status=joined
    #[serde(skip_serializing_if = "Option::is_none")]
    pub convo: Option<ConvoView<S>>,
    pub status: RequestJoinOutputStatus<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


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

impl<S: BosStr> RequestJoinOutputStatus<S> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Joined => "joined",
            Self::Pending => "pending",
            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() {
            "joined" => Self::Joined,
            "pending" => Self::Pending,
            _ => Self::Other(s),
        }
    }
}

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

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

impl<S: BosStr> Serialize for RequestJoinOutputStatus<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 RequestJoinOutputStatus<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 RequestJoinOutputStatus<S> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

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


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

#[serde(tag = "error", content = "message")]
pub enum RequestJoinError {
    #[serde(rename = "ConvoLocked")]
    ConvoLocked(Option<SmolStr>),
    #[serde(rename = "FollowRequired")]
    FollowRequired(Option<SmolStr>),
    #[serde(rename = "InvalidCode")]
    InvalidCode(Option<SmolStr>),
    #[serde(rename = "LinkDisabled")]
    LinkDisabled(Option<SmolStr>),
    #[serde(rename = "MemberLimitReached")]
    MemberLimitReached(Option<SmolStr>),
    #[serde(rename = "UserKicked")]
    UserKicked(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for RequestJoinError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::ConvoLocked(msg) => {
                write!(f, "ConvoLocked")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::FollowRequired(msg) => {
                write!(f, "FollowRequired")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::InvalidCode(msg) => {
                write!(f, "InvalidCode")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::LinkDisabled(msg) => {
                write!(f, "LinkDisabled")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::MemberLimitReached(msg) => {
                write!(f, "MemberLimitReached")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::UserKicked(msg) => {
                write!(f, "UserKicked")?;
                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 `chat.bsky.group.requestJoin` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `RequestJoinOutput<S>` for this endpoint.*/
pub struct RequestJoinResponse;
impl jacquard_common::xrpc::XrpcResp for RequestJoinResponse {
    const NSID: &'static str = "chat.bsky.group.requestJoin";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = RequestJoinOutput<S>;
    type Err = RequestJoinError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for RequestJoin<S> {
    const NSID: &'static str = "chat.bsky.group.requestJoin";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = RequestJoinResponse;
}

/** Endpoint marker for the `chat.bsky.group.requestJoin` procedure.

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