// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: social.showcase.profile.preferences
//
// 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_showcase::ActivitySettings;
use crate::social_showcase::DisplaySettings;
use crate::social_showcase::NotificationSettings;
use crate::social_showcase::PrivacySettings;
use crate::social_showcase::VisibilitySettings;
/// User preferences and settings

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Preferences<'a> {
    #[serde(borrow)]
    pub activity: ActivitySettings<'a>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub display: Option<DisplaySettings<'a>>,
    #[serde(borrow)]
    pub notifications: NotificationSettings<'a>,
    #[serde(borrow)]
    pub privacy: PrivacySettings<'a>,
    ///Schema version for migrations (defaults to 1 if missing)  Defaults to `1`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "_default_preferences_schema_version")]
    pub schema_version: Option<i64>,
    pub updated_at: Datetime,
    #[serde(borrow)]
    pub visibility: VisibilitySettings<'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 PreferencesGetRecordOutput<'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: Preferences<'a>,
}

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

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

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

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

impl Collection for Preferences<'_> {
    const NSID: &'static str = "social.showcase.profile.preferences";
    type Record = PreferencesRecord;
}

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

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

fn _default_preferences_schema_version() -> Option<i64> {
    Some(1i64)
}

pub mod 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 UpdatedAt;
        type Notifications;
        type Visibility;
        type Activity;
        type Privacy;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type UpdatedAt = Unset;
        type Notifications = Unset;
        type Visibility = Unset;
        type Activity = Unset;
        type Privacy = Unset;
    }
    ///State transition - sets the `updated_at` field to Set
    pub struct SetUpdatedAt<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetUpdatedAt<S> {}
    impl<S: State> State for SetUpdatedAt<S> {
        type UpdatedAt = Set<members::updated_at>;
        type Notifications = S::Notifications;
        type Visibility = S::Visibility;
        type Activity = S::Activity;
        type Privacy = S::Privacy;
    }
    ///State transition - sets the `notifications` field to Set
    pub struct SetNotifications<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetNotifications<S> {}
    impl<S: State> State for SetNotifications<S> {
        type UpdatedAt = S::UpdatedAt;
        type Notifications = Set<members::notifications>;
        type Visibility = S::Visibility;
        type Activity = S::Activity;
        type Privacy = S::Privacy;
    }
    ///State transition - sets the `visibility` field to Set
    pub struct SetVisibility<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetVisibility<S> {}
    impl<S: State> State for SetVisibility<S> {
        type UpdatedAt = S::UpdatedAt;
        type Notifications = S::Notifications;
        type Visibility = Set<members::visibility>;
        type Activity = S::Activity;
        type Privacy = S::Privacy;
    }
    ///State transition - sets the `activity` field to Set
    pub struct SetActivity<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetActivity<S> {}
    impl<S: State> State for SetActivity<S> {
        type UpdatedAt = S::UpdatedAt;
        type Notifications = S::Notifications;
        type Visibility = S::Visibility;
        type Activity = Set<members::activity>;
        type Privacy = S::Privacy;
    }
    ///State transition - sets the `privacy` field to Set
    pub struct SetPrivacy<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetPrivacy<S> {}
    impl<S: State> State for SetPrivacy<S> {
        type UpdatedAt = S::UpdatedAt;
        type Notifications = S::Notifications;
        type Visibility = S::Visibility;
        type Activity = S::Activity;
        type Privacy = Set<members::privacy>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `updated_at` field
        pub struct updated_at(());
        ///Marker type for the `notifications` field
        pub struct notifications(());
        ///Marker type for the `visibility` field
        pub struct visibility(());
        ///Marker type for the `activity` field
        pub struct activity(());
        ///Marker type for the `privacy` field
        pub struct privacy(());
    }
}

/// Builder for constructing an instance of this type
pub struct PreferencesBuilder<'a, S: preferences_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<ActivitySettings<'a>>,
        Option<DisplaySettings<'a>>,
        Option<NotificationSettings<'a>>,
        Option<PrivacySettings<'a>>,
        Option<i64>,
        Option<Datetime>,
        Option<VisibilitySettings<'a>>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Preferences<'a> {
    /// Create a new builder for this type
    pub fn new() -> PreferencesBuilder<'a, preferences_state::Empty> {
        PreferencesBuilder::new()
    }
}

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

impl<'a, S> PreferencesBuilder<'a, S>
where
    S: preferences_state::State,
    S::Activity: preferences_state::IsUnset,
{
    /// Set the `activity` field (required)
    pub fn activity(
        mut self,
        value: impl Into<ActivitySettings<'a>>,
    ) -> PreferencesBuilder<'a, preferences_state::SetActivity<S>> {
        self._fields.0 = Option::Some(value.into());
        PreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: preferences_state::State> PreferencesBuilder<'a, S> {
    /// Set the `display` field (optional)
    pub fn display(mut self, value: impl Into<Option<DisplaySettings<'a>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `display` field to an Option value (optional)
    pub fn maybe_display(mut self, value: Option<DisplaySettings<'a>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S> PreferencesBuilder<'a, S>
where
    S: preferences_state::State,
    S::Notifications: preferences_state::IsUnset,
{
    /// Set the `notifications` field (required)
    pub fn notifications(
        mut self,
        value: impl Into<NotificationSettings<'a>>,
    ) -> PreferencesBuilder<'a, preferences_state::SetNotifications<S>> {
        self._fields.2 = Option::Some(value.into());
        PreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> PreferencesBuilder<'a, S>
where
    S: preferences_state::State,
    S::Privacy: preferences_state::IsUnset,
{
    /// Set the `privacy` field (required)
    pub fn privacy(
        mut self,
        value: impl Into<PrivacySettings<'a>>,
    ) -> PreferencesBuilder<'a, preferences_state::SetPrivacy<S>> {
        self._fields.3 = Option::Some(value.into());
        PreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: preferences_state::State> PreferencesBuilder<'a, S> {
    /// Set the `schemaVersion` field (optional)
    pub fn schema_version(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `schemaVersion` field to an Option value (optional)
    pub fn maybe_schema_version(mut self, value: Option<i64>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<'a, S> PreferencesBuilder<'a, S>
where
    S: preferences_state::State,
    S::UpdatedAt: preferences_state::IsUnset,
{
    /// Set the `updatedAt` field (required)
    pub fn updated_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> PreferencesBuilder<'a, preferences_state::SetUpdatedAt<S>> {
        self._fields.5 = Option::Some(value.into());
        PreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> PreferencesBuilder<'a, S>
where
    S: preferences_state::State,
    S::Visibility: preferences_state::IsUnset,
{
    /// Set the `visibility` field (required)
    pub fn visibility(
        mut self,
        value: impl Into<VisibilitySettings<'a>>,
    ) -> PreferencesBuilder<'a, preferences_state::SetVisibility<S>> {
        self._fields.6 = Option::Some(value.into());
        PreferencesBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> PreferencesBuilder<'a, S>
where
    S: preferences_state::State,
    S::UpdatedAt: preferences_state::IsSet,
    S::Notifications: preferences_state::IsSet,
    S::Visibility: preferences_state::IsSet,
    S::Activity: preferences_state::IsSet,
    S::Privacy: preferences_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Preferences<'a> {
        Preferences {
            activity: self._fields.0.unwrap(),
            display: self._fields.1,
            notifications: self._fields.2.unwrap(),
            privacy: self._fields.3.unwrap(),
            schema_version: self._fields.4.or_else(|| Some(1i64)),
            updated_at: self._fields.5.unwrap(),
            visibility: self._fields.6.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>,
        >,
    ) -> Preferences<'a> {
        Preferences {
            activity: self._fields.0.unwrap(),
            display: self._fields.1,
            notifications: self._fields.2.unwrap(),
            privacy: self._fields.3.unwrap(),
            schema_version: self._fields.4.or_else(|| Some(1i64)),
            updated_at: self._fields.5.unwrap(),
            visibility: self._fields.6.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_social_showcase_profile_preferences() -> 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.showcase.profile.preferences"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static("User preferences and settings"),
                    ),
                    key: Some(CowStr::new_static("literal:self")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("visibility"),
                                SmolStr::new_static("activity"),
                                SmolStr::new_static("notifications"),
                                SmolStr::new_static("privacy"),
                                SmolStr::new_static("updatedAt")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("activity"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static(
                                        "social.showcase.defs#activitySettings",
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("display"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static(
                                        "social.showcase.defs#displaySettings",
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("notifications"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static(
                                        "social.showcase.defs#notificationSettings",
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("privacy"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static(
                                        "social.showcase.defs#privacySettings",
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("schemaVersion"),
                                LexObjectProperty::Integer(LexInteger {
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("updatedAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("visibility"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static(
                                        "social.showcase.defs#visibilitySettings",
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}