// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.stream.multistream.createTarget
//
// 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::place_stream::multistream::TargetView;
use crate::place_stream::multistream::target::Target;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct CreateTarget<S: BosStr = DefaultStr> {
    pub multistream_target: Target<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)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct CreateTargetOutput<S: BosStr = DefaultStr> {
    #[serde(flatten)]
    pub value: TargetView<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,
    thiserror::Error,
    miette::Diagnostic
)]

#[serde(tag = "error", content = "message")]
pub enum CreateTargetError {
    /// The provided target URL is invalid or unreachable.
    #[serde(rename = "InvalidTargetUrl")]
    InvalidTargetUrl(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for CreateTargetError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidTargetUrl(msg) => {
                write!(f, "InvalidTargetUrl")?;
                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 `place.stream.multistream.createTarget` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `CreateTargetOutput<S>` for this endpoint.*/
pub struct CreateTargetResponse;
impl jacquard_common::xrpc::XrpcResp for CreateTargetResponse {
    const NSID: &'static str = "place.stream.multistream.createTarget";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = CreateTargetOutput<S>;
    type Err = CreateTargetError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for CreateTarget<S> {
    const NSID: &'static str = "place.stream.multistream.createTarget";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = CreateTargetResponse;
}

/** Endpoint marker for the `place.stream.multistream.createTarget` procedure.

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

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

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

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

impl<S: BosStr> CreateTarget<S> {
    /// Create a new builder for this type
    pub fn builder() -> CreateTargetBuilder<create_target_state::Empty, S> {
        CreateTargetBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> CreateTargetBuilder<St, S>
where
    St: create_target_state::State,
    St::MultistreamTarget: create_target_state::IsUnset,
{
    /// Set the `multistreamTarget` field (required)
    pub fn multistream_target(
        mut self,
        value: impl Into<Target<S>>,
    ) -> CreateTargetBuilder<create_target_state::SetMultistreamTarget<St>, S> {
        self._fields.0 = Option::Some(value.into());
        CreateTargetBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> CreateTargetBuilder<St, S>
where
    St: create_target_state::State,
    St::MultistreamTarget: create_target_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> CreateTarget<S> {
        CreateTarget {
            multistream_target: self._fields.0.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>>,
    ) -> CreateTarget<S> {
        CreateTarget {
            multistream_target: self._fields.0.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}