// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: app.bsky.graph.getRelationships
//
// 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::ident::AtIdentifier;
use jacquard_common::types::string::Did;
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::app_bsky::graph::NotFoundActor;
use crate::app_bsky::graph::Relationship;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct GetRelationships<S: BosStr = DefaultStr> {
    pub actor: AtIdentifier<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub others: Option<Vec<AtIdentifier<S>>>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct GetRelationshipsOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actor: Option<Did<S>>,
    pub relationships: Vec<GetRelationshipsOutputRelationshipsItem<S>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[open_union]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(tag = "$type", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub enum GetRelationshipsOutputRelationshipsItem<S: BosStr = DefaultStr> {
    #[serde(rename = "app.bsky.graph.defs#relationship")]
    Relationship(Box<Relationship<S>>),
    #[serde(rename = "app.bsky.graph.defs#notFoundActor")]
    NotFoundActor(Box<NotFoundActor<S>>),
}


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

#[serde(tag = "error", content = "message")]
pub enum GetRelationshipsError {
    /// the primary actor at-identifier could not be resolved
    #[serde(rename = "ActorNotFound")]
    ActorNotFound(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for GetRelationshipsError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::ActorNotFound(msg) => {
                write!(f, "ActorNotFound")?;
                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 `app.bsky.graph.getRelationships` query.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `GetRelationshipsOutput<S>` for this endpoint.*/
pub struct GetRelationshipsResponse;
impl jacquard_common::xrpc::XrpcResp for GetRelationshipsResponse {
    const NSID: &'static str = "app.bsky.graph.getRelationships";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = GetRelationshipsOutput<S>;
    type Err = GetRelationshipsError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for GetRelationships<S> {
    const NSID: &'static str = "app.bsky.graph.getRelationships";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Response = GetRelationshipsResponse;
}

/** Endpoint marker for the `app.bsky.graph.getRelationships` query.

Path: `/xrpc/app.bsky.graph.getRelationships`. The request payload type is `GetRelationships<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
pub struct GetRelationshipsRequest;
impl jacquard_common::xrpc::XrpcEndpoint for GetRelationshipsRequest {
    const PATH: &'static str = "/xrpc/app.bsky.graph.getRelationships";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Request<S: BosStr> = GetRelationships<S>;
    type Response = GetRelationshipsResponse;
}

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

/// Builder for constructing an instance of this type.
pub struct GetRelationshipsBuilder<
    St: get_relationships_state::State,
    S: BosStr = DefaultStr,
> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<AtIdentifier<S>>, Option<Vec<AtIdentifier<S>>>),
    _type: PhantomData<fn() -> S>,
}

impl GetRelationships<DefaultStr> {
    /// Create a new builder for this type, using the default string type (DefaultStr = SmolStr) if needed
    pub fn new() -> GetRelationshipsBuilder<get_relationships_state::Empty, DefaultStr> {
        GetRelationshipsBuilder::new()
    }
}

impl<S: BosStr> GetRelationships<S> {
    /// Create a new builder for this type
    pub fn builder() -> GetRelationshipsBuilder<get_relationships_state::Empty, S> {
        GetRelationshipsBuilder::builder()
    }
}

impl GetRelationshipsBuilder<get_relationships_state::Empty, DefaultStr> {
    /// Create a new builder with all fields unset, using the default string type, if needed
    pub fn new() -> Self {
        GetRelationshipsBuilder {
            _state: PhantomData,
            _fields: (None, None),
            _type: PhantomData,
        }
    }
}

impl<S: BosStr> GetRelationshipsBuilder<get_relationships_state::Empty, S> {
    /// Create a new builder with all fields unset
    pub fn builder() -> Self {
        GetRelationshipsBuilder {
            _state: PhantomData,
            _fields: (None, None),
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> GetRelationshipsBuilder<St, S>
where
    St: get_relationships_state::State,
    St::Actor: get_relationships_state::IsUnset,
{
    /// Set the `actor` field (required)
    pub fn actor(
        mut self,
        value: impl Into<AtIdentifier<S>>,
    ) -> GetRelationshipsBuilder<get_relationships_state::SetActor<St>, S> {
        self._fields.0 = Option::Some(value.into());
        GetRelationshipsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: get_relationships_state::State, S: BosStr> GetRelationshipsBuilder<St, S> {
    /// Set the `others` field (optional)
    pub fn others(mut self, value: impl Into<Option<Vec<AtIdentifier<S>>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `others` field to an Option value (optional)
    pub fn maybe_others(mut self, value: Option<Vec<AtIdentifier<S>>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> GetRelationshipsBuilder<St, S>
where
    St: get_relationships_state::State,
    St::Actor: get_relationships_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> GetRelationships<S> {
        GetRelationships {
            actor: self._fields.0.unwrap(),
            others: self._fields.1,
        }
    }
}