// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: social.tophhie.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, Datetime};
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::social_tophhie::profile;
/// Granular communication consent flags.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct CommunicationPreferences<'a> {
    ///True if the user consents to receive marketing communications.
    pub marketing: bool,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Profile<'a> {
    ///User's opt-in/out preferences for communications.
    #[serde(borrow)]
    pub communication_preferences: profile::CommunicationPreferences<'a>,
    ///ISO 8601 timestamp when this profile record was created.
    pub created_at: Datetime,
    ///User's opt-in/out preferences for PDS features.
    #[serde(borrow)]
    pub pds_preferences: profile::PdsPreferences<'a>,
    ///ISO 8601 timestamp when this profile record was updated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<Datetime>,
}

/// 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>,
}

/// Granular PDS preference consent flags.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct PdsPreferences<'a> {
    ///True if the user participates in accessibility scoring.
    pub accessibility_scoring: bool,
    ///True if the user is shown on the Tophhie Social homepage.
    pub show_on_homepage: bool,
}

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 CommunicationPreferences<'a> {
    fn nsid() -> &'static str {
        "social.tophhie.profile"
    }
    fn def_name() -> &'static str {
        "communicationPreferences"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_social_tophhie_profile()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

#[derive(Debug, Serialize, Deserialize)]
pub struct ProfileRecord;
impl XrpcResp for ProfileRecord {
    const NSID: &'static str = "social.tophhie.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 = "social.tophhie.profile";
    type Record = ProfileRecord;
}

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

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

impl<'a> LexiconSchema for PdsPreferences<'a> {
    fn nsid() -> &'static str {
        "social.tophhie.profile"
    }
    fn def_name() -> &'static str {
        "pdsPreferences"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_social_tophhie_profile()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

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

impl<'a> CommunicationPreferences<'a> {
    /// Create a new builder for this type
    pub fn new() -> CommunicationPreferencesBuilder<
        'a,
        communication_preferences_state::Empty,
    > {
        CommunicationPreferencesBuilder::new()
    }
}

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

impl<'a, S> CommunicationPreferencesBuilder<'a, S>
where
    S: communication_preferences_state::State,
    S::Marketing: communication_preferences_state::IsUnset,
{
    /// Set the `marketing` field (required)
    pub fn marketing(
        mut self,
        value: impl Into<bool>,
    ) -> CommunicationPreferencesBuilder<
        'a,
        communication_preferences_state::SetMarketing<S>,
    > {
        self._fields.0 = Option::Some(value.into());
        CommunicationPreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> CommunicationPreferencesBuilder<'a, S>
where
    S: communication_preferences_state::State,
    S::Marketing: communication_preferences_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> CommunicationPreferences<'a> {
        CommunicationPreferences {
            marketing: 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<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> CommunicationPreferences<'a> {
        CommunicationPreferences {
            marketing: self._fields.0.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_social_tophhie_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("social.tophhie.profile"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("communicationPreferences"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static("Granular communication consent flags."),
                    ),
                    required: Some(vec![SmolStr::new_static("marketing")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("marketing"),
                            LexObjectProperty::Boolean(LexBoolean {
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    key: Some(CowStr::new_static("literal:self")),
                    record: LexRecordRecord::Object(LexObject {
                        description: Some(CowStr::new_static("Tophhie profile record")),
                        required: Some(
                            vec![
                                SmolStr::new_static("createdAt"),
                                SmolStr::new_static("pdsPreferences"),
                                SmolStr::new_static("communicationPreferences")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("communicationPreferences"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("#communicationPreferences"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "ISO 8601 timestamp when this profile record was created.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("pdsPreferences"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("#pdsPreferences"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("updatedAt"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "ISO 8601 timestamp when this profile record was updated.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("pdsPreferences"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static("Granular PDS preference consent flags."),
                    ),
                    required: Some(
                        vec![
                            SmolStr::new_static("showOnHomepage"),
                            SmolStr::new_static("accessibilityScoring")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("accessibilityScoring"),
                            LexObjectProperty::Boolean(LexBoolean {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("showOnHomepage"),
                            LexObjectProperty::Boolean(LexBoolean {
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..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 {
        type CreatedAt;
        type CommunicationPreferences;
        type PdsPreferences;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type CreatedAt = Unset;
        type CommunicationPreferences = Unset;
        type PdsPreferences = Unset;
    }
    ///State transition - sets the `created_at` field to Set
    pub struct SetCreatedAt<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetCreatedAt<S> {}
    impl<S: State> State for SetCreatedAt<S> {
        type CreatedAt = Set<members::created_at>;
        type CommunicationPreferences = S::CommunicationPreferences;
        type PdsPreferences = S::PdsPreferences;
    }
    ///State transition - sets the `communication_preferences` field to Set
    pub struct SetCommunicationPreferences<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetCommunicationPreferences<S> {}
    impl<S: State> State for SetCommunicationPreferences<S> {
        type CreatedAt = S::CreatedAt;
        type CommunicationPreferences = Set<members::communication_preferences>;
        type PdsPreferences = S::PdsPreferences;
    }
    ///State transition - sets the `pds_preferences` field to Set
    pub struct SetPdsPreferences<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetPdsPreferences<S> {}
    impl<S: State> State for SetPdsPreferences<S> {
        type CreatedAt = S::CreatedAt;
        type CommunicationPreferences = S::CommunicationPreferences;
        type PdsPreferences = Set<members::pds_preferences>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `created_at` field
        pub struct created_at(());
        ///Marker type for the `communication_preferences` field
        pub struct communication_preferences(());
        ///Marker type for the `pds_preferences` field
        pub struct pds_preferences(());
    }
}

/// Builder for constructing an instance of this type
pub struct ProfileBuilder<'a, S: profile_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<profile::CommunicationPreferences<'a>>,
        Option<Datetime>,
        Option<profile::PdsPreferences<'a>>,
        Option<Datetime>,
    ),
    _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, None, None, None),
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ProfileBuilder<'a, S>
where
    S: profile_state::State,
    S::CommunicationPreferences: profile_state::IsUnset,
{
    /// Set the `communicationPreferences` field (required)
    pub fn communication_preferences(
        mut self,
        value: impl Into<profile::CommunicationPreferences<'a>>,
    ) -> ProfileBuilder<'a, profile_state::SetCommunicationPreferences<S>> {
        self._fields.0 = Option::Some(value.into());
        ProfileBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ProfileBuilder<'a, S>
where
    S: profile_state::State,
    S::CreatedAt: profile_state::IsUnset,
{
    /// Set the `createdAt` field (required)
    pub fn created_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> ProfileBuilder<'a, profile_state::SetCreatedAt<S>> {
        self._fields.1 = Option::Some(value.into());
        ProfileBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ProfileBuilder<'a, S>
where
    S: profile_state::State,
    S::PdsPreferences: profile_state::IsUnset,
{
    /// Set the `pdsPreferences` field (required)
    pub fn pds_preferences(
        mut self,
        value: impl Into<profile::PdsPreferences<'a>>,
    ) -> ProfileBuilder<'a, profile_state::SetPdsPreferences<S>> {
        self._fields.2 = Option::Some(value.into());
        ProfileBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: profile_state::State> ProfileBuilder<'a, S> {
    /// Set the `updatedAt` field (optional)
    pub fn updated_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `updatedAt` field to an Option value (optional)
    pub fn maybe_updated_at(mut self, value: Option<Datetime>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<'a, S> ProfileBuilder<'a, S>
where
    S: profile_state::State,
    S::CreatedAt: profile_state::IsSet,
    S::CommunicationPreferences: profile_state::IsSet,
    S::PdsPreferences: profile_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Profile<'a> {
        Profile {
            communication_preferences: self._fields.0.unwrap(),
            created_at: self._fields.1.unwrap(),
            pds_preferences: self._fields.2.unwrap(),
            updated_at: self._fields.3,
            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 {
            communication_preferences: self._fields.0.unwrap(),
            created_at: self._fields.1.unwrap(),
            pds_preferences: self._fields.2.unwrap(),
            updated_at: self._fields.3,
            extra_data: Some(extra_data),
        }
    }
}

pub mod pds_preferences_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 ShowOnHomepage;
        type AccessibilityScoring;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type ShowOnHomepage = Unset;
        type AccessibilityScoring = Unset;
    }
    ///State transition - sets the `show_on_homepage` field to Set
    pub struct SetShowOnHomepage<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetShowOnHomepage<S> {}
    impl<S: State> State for SetShowOnHomepage<S> {
        type ShowOnHomepage = Set<members::show_on_homepage>;
        type AccessibilityScoring = S::AccessibilityScoring;
    }
    ///State transition - sets the `accessibility_scoring` field to Set
    pub struct SetAccessibilityScoring<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetAccessibilityScoring<S> {}
    impl<S: State> State for SetAccessibilityScoring<S> {
        type ShowOnHomepage = S::ShowOnHomepage;
        type AccessibilityScoring = Set<members::accessibility_scoring>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `show_on_homepage` field
        pub struct show_on_homepage(());
        ///Marker type for the `accessibility_scoring` field
        pub struct accessibility_scoring(());
    }
}

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

impl<'a> PdsPreferences<'a> {
    /// Create a new builder for this type
    pub fn new() -> PdsPreferencesBuilder<'a, pds_preferences_state::Empty> {
        PdsPreferencesBuilder::new()
    }
}

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

impl<'a, S> PdsPreferencesBuilder<'a, S>
where
    S: pds_preferences_state::State,
    S::AccessibilityScoring: pds_preferences_state::IsUnset,
{
    /// Set the `accessibilityScoring` field (required)
    pub fn accessibility_scoring(
        mut self,
        value: impl Into<bool>,
    ) -> PdsPreferencesBuilder<'a, pds_preferences_state::SetAccessibilityScoring<S>> {
        self._fields.0 = Option::Some(value.into());
        PdsPreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> PdsPreferencesBuilder<'a, S>
where
    S: pds_preferences_state::State,
    S::ShowOnHomepage: pds_preferences_state::IsUnset,
{
    /// Set the `showOnHomepage` field (required)
    pub fn show_on_homepage(
        mut self,
        value: impl Into<bool>,
    ) -> PdsPreferencesBuilder<'a, pds_preferences_state::SetShowOnHomepage<S>> {
        self._fields.1 = Option::Some(value.into());
        PdsPreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> PdsPreferencesBuilder<'a, S>
where
    S: pds_preferences_state::State,
    S::ShowOnHomepage: pds_preferences_state::IsSet,
    S::AccessibilityScoring: pds_preferences_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> PdsPreferences<'a> {
        PdsPreferences {
            accessibility_scoring: self._fields.0.unwrap(),
            show_on_homepage: 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<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> PdsPreferences<'a> {
        PdsPreferences {
            accessibility_scoring: self._fields.0.unwrap(),
            show_on_homepage: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}