// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: tools.ozone.queue.createQueue
//
// 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::Nsid;
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::tools_ozone::queue::QueueView;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct CreateQueue<S: BosStr = DefaultStr> {
    ///Collection name for record subjects. Required if subjectTypes includes 'record'.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collection: Option<Nsid<S>>,
    ///Optional description of the queue
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<S>,
    ///Display name for the queue (must be unique)
    pub name: S,
    ///Report reason types (fully qualified NSIDs)
    pub report_types: Vec<S>,
    ///Subject types this queue accepts
    pub subject_types: Vec<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 CreateQueueOutput<S: BosStr = DefaultStr> {
    pub queue: QueueView<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 CreateQueueError {
    /// The queue configuration conflicts with an existing queue
    #[serde(rename = "ConflictingQueue")]
    ConflictingQueue(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for CreateQueueError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::ConflictingQueue(msg) => {
                write!(f, "ConflictingQueue")?;
                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 `tools.ozone.queue.createQueue` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `CreateQueueOutput<S>` for this endpoint.*/
pub struct CreateQueueResponse;
impl jacquard_common::xrpc::XrpcResp for CreateQueueResponse {
    const NSID: &'static str = "tools.ozone.queue.createQueue";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = CreateQueueOutput<S>;
    type Err = CreateQueueError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for CreateQueue<S> {
    const NSID: &'static str = "tools.ozone.queue.createQueue";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = CreateQueueResponse;
}

/** Endpoint marker for the `tools.ozone.queue.createQueue` procedure.

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

pub mod create_queue_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 Name;
        type ReportTypes;
        type SubjectTypes;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Name = Unset;
        type ReportTypes = Unset;
        type SubjectTypes = Unset;
    }
    ///State transition - sets the `name` field to Set
    pub struct SetName<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetName<St> {}
    impl<St: State> State for SetName<St> {
        type Name = Set<members::name>;
        type ReportTypes = St::ReportTypes;
        type SubjectTypes = St::SubjectTypes;
    }
    ///State transition - sets the `report_types` field to Set
    pub struct SetReportTypes<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetReportTypes<St> {}
    impl<St: State> State for SetReportTypes<St> {
        type Name = St::Name;
        type ReportTypes = Set<members::report_types>;
        type SubjectTypes = St::SubjectTypes;
    }
    ///State transition - sets the `subject_types` field to Set
    pub struct SetSubjectTypes<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSubjectTypes<St> {}
    impl<St: State> State for SetSubjectTypes<St> {
        type Name = St::Name;
        type ReportTypes = St::ReportTypes;
        type SubjectTypes = Set<members::subject_types>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `name` field
        pub struct name(());
        ///Marker type for the `report_types` field
        pub struct report_types(());
        ///Marker type for the `subject_types` field
        pub struct subject_types(());
    }
}

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

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

impl<S: BosStr> CreateQueue<S> {
    /// Create a new builder for this type
    pub fn builder() -> CreateQueueBuilder<create_queue_state::Empty, S> {
        CreateQueueBuilder::builder()
    }
}

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

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

impl<St: create_queue_state::State, S: BosStr> CreateQueueBuilder<St, S> {
    /// Set the `collection` field (optional)
    pub fn collection(mut self, value: impl Into<Option<Nsid<S>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `collection` field to an Option value (optional)
    pub fn maybe_collection(mut self, value: Option<Nsid<S>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<St: create_queue_state::State, S: BosStr> CreateQueueBuilder<St, S> {
    /// Set the `description` field (optional)
    pub fn description(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `description` field to an Option value (optional)
    pub fn maybe_description(mut self, value: Option<S>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> CreateQueueBuilder<St, S>
where
    St: create_queue_state::State,
    St::Name: create_queue_state::IsUnset,
{
    /// Set the `name` field (required)
    pub fn name(
        mut self,
        value: impl Into<S>,
    ) -> CreateQueueBuilder<create_queue_state::SetName<St>, S> {
        self._fields.2 = Option::Some(value.into());
        CreateQueueBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> CreateQueueBuilder<St, S>
where
    St: create_queue_state::State,
    St::ReportTypes: create_queue_state::IsUnset,
{
    /// Set the `reportTypes` field (required)
    pub fn report_types(
        mut self,
        value: impl Into<Vec<S>>,
    ) -> CreateQueueBuilder<create_queue_state::SetReportTypes<St>, S> {
        self._fields.3 = Option::Some(value.into());
        CreateQueueBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> CreateQueueBuilder<St, S>
where
    St: create_queue_state::State,
    St::SubjectTypes: create_queue_state::IsUnset,
{
    /// Set the `subjectTypes` field (required)
    pub fn subject_types(
        mut self,
        value: impl Into<Vec<S>>,
    ) -> CreateQueueBuilder<create_queue_state::SetSubjectTypes<St>, S> {
        self._fields.4 = Option::Some(value.into());
        CreateQueueBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> CreateQueueBuilder<St, S>
where
    St: create_queue_state::State,
    St::Name: create_queue_state::IsSet,
    St::ReportTypes: create_queue_state::IsSet,
    St::SubjectTypes: create_queue_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> CreateQueue<S> {
        CreateQueue {
            collection: self._fields.0,
            description: self._fields.1,
            name: self._fields.2.unwrap(),
            report_types: self._fields.3.unwrap(),
            subject_types: self._fields.4.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>>,
    ) -> CreateQueue<S> {
        CreateQueue {
            collection: self._fields.0,
            description: self._fields.1,
            name: self._fields.2.unwrap(),
            report_types: self._fields.3.unwrap(),
            subject_types: self._fields.4.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}