// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: win.tomo-x.pushat.pushNotify
//
// 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::Did;
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::win_tomo_x::pushat::NotifyBody;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct PushNotify<S: BosStr = DefaultStr> {
    pub body: NotifyBody<S>,
    ///The DID of the target user to whom the notification will be sent.
    pub target: Did<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 PushNotifyOutput<S: BosStr = DefaultStr> {
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


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

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

impl core::fmt::Display for PushNotifyError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::ServiceNotAllowedError(msg) => {
                write!(f, "ServiceNotAllowedError")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::DeviceNotFoundError(msg) => {
                write!(f, "DeviceNotFoundError")?;
                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 `win.tomo-x.pushat.pushNotify` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `PushNotifyOutput<S>` for this endpoint.*/
pub struct PushNotifyResponse;
impl jacquard_common::xrpc::XrpcResp for PushNotifyResponse {
    const NSID: &'static str = "win.tomo-x.pushat.pushNotify";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = PushNotifyOutput<S>;
    type Err = PushNotifyError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for PushNotify<S> {
    const NSID: &'static str = "win.tomo-x.pushat.pushNotify";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = PushNotifyResponse;
}

/** Endpoint marker for the `win.tomo-x.pushat.pushNotify` procedure.

Path: `/xrpc/win.tomo-x.pushat.pushNotify`. The request payload type is `PushNotify<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
pub struct PushNotifyRequest;
impl jacquard_common::xrpc::XrpcEndpoint for PushNotifyRequest {
    const PATH: &'static str = "/xrpc/win.tomo-x.pushat.pushNotify";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<S: BosStr> = PushNotify<S>;
    type Response = PushNotifyResponse;
}

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

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

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

impl<S: BosStr> PushNotify<S> {
    /// Create a new builder for this type
    pub fn builder() -> PushNotifyBuilder<push_notify_state::Empty, S> {
        PushNotifyBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> PushNotifyBuilder<St, S>
where
    St: push_notify_state::State,
    St::Body: push_notify_state::IsUnset,
{
    /// Set the `body` field (required)
    pub fn body(
        mut self,
        value: impl Into<NotifyBody<S>>,
    ) -> PushNotifyBuilder<push_notify_state::SetBody<St>, S> {
        self._fields.0 = Option::Some(value.into());
        PushNotifyBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> PushNotifyBuilder<St, S>
where
    St: push_notify_state::State,
    St::Target: push_notify_state::IsUnset,
{
    /// Set the `target` field (required)
    pub fn target(
        mut self,
        value: impl Into<Did<S>>,
    ) -> PushNotifyBuilder<push_notify_state::SetTarget<St>, S> {
        self._fields.1 = Option::Some(value.into());
        PushNotifyBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> PushNotifyBuilder<St, S>
where
    St: push_notify_state::State,
    St::Body: push_notify_state::IsSet,
    St::Target: push_notify_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> PushNotify<S> {
        PushNotify {
            body: self._fields.0.unwrap(),
            target: 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<SmolStr, Data<S>>,
    ) -> PushNotify<S> {
        PushNotify {
            body: self._fields.0.unwrap(),
            target: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}