// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.stream.chat.profile
//
// 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;

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::xrpc::XrpcResp;
use jacquard_derive::{IntoStatic, lexicon};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
use crate::place_stream::chat::profile;
/// Customizations for the color of a user's name in chat

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Color<'a> {
    pub blue: i64,
    pub green: i64,
    pub red: i64,
}

/// Record containing customizations for a user's chat profile.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Profile<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub color: Option<profile::Color<'a>>,
}

/// Typed wrapper for GetRecord response with this collection's record type.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct ProfileGetRecordOutput<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
    #[serde(borrow)]
    pub uri: AtUri<'a>,
    #[serde(borrow)]
    pub value: Profile<'a>,
}

impl<'a> Profile<'a> {
    pub fn uri(
        uri: impl Into<CowStr<'a>>,
    ) -> Result<RecordUri<'a, ProfileRecord>, UriError> {
        RecordUri::try_from_uri(AtUri::new_cow(uri.into())?)
    }
}

impl<'a> LexiconSchema for Color<'a> {
    fn nsid() -> &'static str {
        "place.stream.chat.profile"
    }
    fn def_name() -> &'static str {
        "color"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_chat_profile()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.blue;
            if *value > 255i64 {
                return Err(ConstraintError::Maximum {
                    path: ValidationPath::from_field("blue"),
                    max: 255i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.blue;
            if *value < 0i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("blue"),
                    min: 0i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.green;
            if *value > 255i64 {
                return Err(ConstraintError::Maximum {
                    path: ValidationPath::from_field("green"),
                    max: 255i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.green;
            if *value < 0i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("green"),
                    min: 0i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.red;
            if *value > 255i64 {
                return Err(ConstraintError::Maximum {
                    path: ValidationPath::from_field("red"),
                    max: 255i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.red;
            if *value < 0i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("red"),
                    min: 0i64,
                    actual: *value,
                });
            }
        }
        Ok(())
    }
}

/// Marker type for deserializing records from this collection.

#[derive(Debug, Serialize, Deserialize)]
pub struct ProfileRecord;
impl XrpcResp for ProfileRecord {
    const NSID: &'static str = "place.stream.chat.profile";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = ProfileGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

impl From<ProfileGetRecordOutput<'_>> for Profile<'_> {
    fn from(output: ProfileGetRecordOutput<'_>) -> Self {
        use jacquard_common::IntoStatic;
        output.value.into_static()
    }
}

impl Collection for Profile<'_> {
    const NSID: &'static str = "place.stream.chat.profile";
    type Record = ProfileRecord;
}

impl Collection for ProfileRecord {
    const NSID: &'static str = "place.stream.chat.profile";
    type Record = ProfileRecord;
}

impl<'a> LexiconSchema for Profile<'a> {
    fn nsid() -> &'static str {
        "place.stream.chat.profile"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_chat_profile()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

pub mod color_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 Blue;
        type Red;
        type Green;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Blue = Unset;
        type Red = Unset;
        type Green = Unset;
    }
    ///State transition - sets the `blue` field to Set
    pub struct SetBlue<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetBlue<S> {}
    impl<S: State> State for SetBlue<S> {
        type Blue = Set<members::blue>;
        type Red = S::Red;
        type Green = S::Green;
    }
    ///State transition - sets the `red` field to Set
    pub struct SetRed<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetRed<S> {}
    impl<S: State> State for SetRed<S> {
        type Blue = S::Blue;
        type Red = Set<members::red>;
        type Green = S::Green;
    }
    ///State transition - sets the `green` field to Set
    pub struct SetGreen<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetGreen<S> {}
    impl<S: State> State for SetGreen<S> {
        type Blue = S::Blue;
        type Red = S::Red;
        type Green = Set<members::green>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `blue` field
        pub struct blue(());
        ///Marker type for the `red` field
        pub struct red(());
        ///Marker type for the `green` field
        pub struct green(());
    }
}

/// Builder for constructing an instance of this type
pub struct ColorBuilder<'a, S: color_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<i64>, Option<i64>, Option<i64>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Color<'a> {
    /// Create a new builder for this type
    pub fn new() -> ColorBuilder<'a, color_state::Empty> {
        ColorBuilder::new()
    }
}

impl<'a> ColorBuilder<'a, color_state::Empty> {
    /// Create a new builder with all fields unset
    pub fn new() -> Self {
        ColorBuilder {
            _state: PhantomData,
            _fields: (None, None, None),
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ColorBuilder<'a, S>
where
    S: color_state::State,
    S::Blue: color_state::IsUnset,
{
    /// Set the `blue` field (required)
    pub fn blue(
        mut self,
        value: impl Into<i64>,
    ) -> ColorBuilder<'a, color_state::SetBlue<S>> {
        self._fields.0 = Option::Some(value.into());
        ColorBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ColorBuilder<'a, S>
where
    S: color_state::State,
    S::Green: color_state::IsUnset,
{
    /// Set the `green` field (required)
    pub fn green(
        mut self,
        value: impl Into<i64>,
    ) -> ColorBuilder<'a, color_state::SetGreen<S>> {
        self._fields.1 = Option::Some(value.into());
        ColorBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ColorBuilder<'a, S>
where
    S: color_state::State,
    S::Red: color_state::IsUnset,
{
    /// Set the `red` field (required)
    pub fn red(
        mut self,
        value: impl Into<i64>,
    ) -> ColorBuilder<'a, color_state::SetRed<S>> {
        self._fields.2 = Option::Some(value.into());
        ColorBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ColorBuilder<'a, S>
where
    S: color_state::State,
    S::Blue: color_state::IsSet,
    S::Red: color_state::IsSet,
    S::Green: color_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Color<'a> {
        Color {
            blue: self._fields.0.unwrap(),
            green: self._fields.1.unwrap(),
            red: self._fields.2.unwrap(),
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Color<'a> {
        Color {
            blue: self._fields.0.unwrap(),
            green: self._fields.1.unwrap(),
            red: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_place_stream_chat_profile() -> LexiconDoc<'static> {
    #[allow(unused_imports)]
    use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
    use jacquard_lexicon::lexicon::*;
    use alloc::collections::BTreeMap;
    LexiconDoc {
        lexicon: Lexicon::Lexicon1,
        id: CowStr::new_static("place.stream.chat.profile"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("color"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "Customizations for the color of a user's name in chat",
                        ),
                    ),
                    required: Some(
                        vec![
                            SmolStr::new_static("red"), SmolStr::new_static("green"),
                            SmolStr::new_static("blue")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("blue"),
                            LexObjectProperty::Integer(LexInteger {
                                minimum: Some(0i64),
                                maximum: Some(255i64),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("green"),
                            LexObjectProperty::Integer(LexInteger {
                                minimum: Some(0i64),
                                maximum: Some(255i64),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("red"),
                            LexObjectProperty::Integer(LexInteger {
                                minimum: Some(0i64),
                                maximum: Some(255i64),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "Record containing customizations for a user's chat profile.",
                        ),
                    ),
                    key: Some(CowStr::new_static("literal:self")),
                    record: LexRecordRecord::Object(LexObject {
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("color"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("#color"),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

pub mod profile_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 {}
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {}
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {}
}

/// Builder for constructing an instance of this type
pub struct ProfileBuilder<'a, S: profile_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<profile::Color<'a>>,),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Profile<'a> {
    /// Create a new builder for this type
    pub fn new() -> ProfileBuilder<'a, profile_state::Empty> {
        ProfileBuilder::new()
    }
}

impl<'a> ProfileBuilder<'a, profile_state::Empty> {
    /// Create a new builder with all fields unset
    pub fn new() -> Self {
        ProfileBuilder {
            _state: PhantomData,
            _fields: (None,),
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: profile_state::State> ProfileBuilder<'a, S> {
    /// Set the `color` field (optional)
    pub fn color(mut self, value: impl Into<Option<profile::Color<'a>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `color` field to an Option value (optional)
    pub fn maybe_color(mut self, value: Option<profile::Color<'a>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S> ProfileBuilder<'a, S>
where
    S: profile_state::State,
{
    /// Build the final struct
    pub fn build(self) -> Profile<'a> {
        Profile {
            color: self._fields.0,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Profile<'a> {
        Profile {
            color: self._fields.0,
            extra_data: Some(extra_data),
        }
    }
}