// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: com.atprotofans.hydratedProfile
//
// 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::blob::BlobRef;
use jacquard_common::types::string::{Did, Handle};
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::app_bsky::richtext::facet::Facet;
/// A hydrated identity profile with computed fields.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct HydratedProfile<'a> {
    ///Whether the identity is currently accepting new supporters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accepting_supporters: Option<bool>,
    ///Avatar image blob reference.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub avatar: Option<BlobRef<'a>>,
    ///Banner image blob reference.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub banner: Option<BlobRef<'a>>,
    ///Profile bio/description with optional rich text.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<CowStr<'a>>,
    ///DID of the profile owner.
    #[serde(borrow)]
    pub did: Did<'a>,
    ///Display name for the profile.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub display_name: Option<CowStr<'a>>,
    ///Rich text facets for description annotations.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub facets: Option<Vec<Facet<'a>>>,
    ///Handle of the profile owner.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub handle: Option<Handle<'a>>,
    ///Number of supporters for this profile.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supporter_count: Option<i64>,
}

impl<'a> LexiconSchema for HydratedProfile<'a> {
    fn nsid() -> &'static str {
        "com.atprotofans.hydratedProfile"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_com_atprotofans_hydratedProfile()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.avatar {
            {
                let size = value.blob().size;
                if size > 1000000usize {
                    return Err(ConstraintError::BlobTooLarge {
                        path: ValidationPath::from_field("avatar"),
                        max: 1000000usize,
                        actual: size,
                    });
                }
            }
        }
        if let Some(ref value) = self.avatar {
            {
                let mime = value.blob().mime_type.as_str();
                let accepted: &[&str] = &["image/png", "image/jpeg", "image/webp"];
                let matched = accepted
                    .iter()
                    .any(|pattern| {
                        if *pattern == "*/*" {
                            true
                        } else if pattern.ends_with("/*") {
                            let prefix = &pattern[..pattern.len() - 2];
                            mime.starts_with(prefix)
                                && mime.as_bytes().get(prefix.len()) == Some(&b'/')
                        } else {
                            mime == *pattern
                        }
                    });
                if !matched {
                    return Err(ConstraintError::BlobMimeTypeNotAccepted {
                        path: ValidationPath::from_field("avatar"),
                        accepted: vec![
                            "image/png".to_string(), "image/jpeg".to_string(),
                            "image/webp".to_string()
                        ],
                        actual: mime.to_string(),
                    });
                }
            }
        }
        if let Some(ref value) = self.banner {
            {
                let size = value.blob().size;
                if size > 1000000usize {
                    return Err(ConstraintError::BlobTooLarge {
                        path: ValidationPath::from_field("banner"),
                        max: 1000000usize,
                        actual: size,
                    });
                }
            }
        }
        if let Some(ref value) = self.banner {
            {
                let mime = value.blob().mime_type.as_str();
                let accepted: &[&str] = &["image/png", "image/jpeg", "image/webp"];
                let matched = accepted
                    .iter()
                    .any(|pattern| {
                        if *pattern == "*/*" {
                            true
                        } else if pattern.ends_with("/*") {
                            let prefix = &pattern[..pattern.len() - 2];
                            mime.starts_with(prefix)
                                && mime.as_bytes().get(prefix.len()) == Some(&b'/')
                        } else {
                            mime == *pattern
                        }
                    });
                if !matched {
                    return Err(ConstraintError::BlobMimeTypeNotAccepted {
                        path: ValidationPath::from_field("banner"),
                        accepted: vec![
                            "image/png".to_string(), "image/jpeg".to_string(),
                            "image/webp".to_string()
                        ],
                        actual: mime.to_string(),
                    });
                }
            }
        }
        if let Some(ref value) = self.description {
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 2560usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("description"),
                    max: 2560usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        if let Some(ref value) = self.description {
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 256usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("description"),
                        max: 256usize,
                        actual: count,
                    });
                }
            }
        }
        if let Some(ref value) = self.display_name {
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 640usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("display_name"),
                    max: 640usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        if let Some(ref value) = self.display_name {
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 64usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("display_name"),
                        max: 64usize,
                        actual: count,
                    });
                }
            }
        }
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type
pub struct HydratedProfileBuilder<'a, S: hydrated_profile_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<bool>,
        Option<BlobRef<'a>>,
        Option<BlobRef<'a>>,
        Option<CowStr<'a>>,
        Option<Did<'a>>,
        Option<CowStr<'a>>,
        Option<Vec<Facet<'a>>>,
        Option<Handle<'a>>,
        Option<i64>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> HydratedProfile<'a> {
    /// Create a new builder for this type
    pub fn new() -> HydratedProfileBuilder<'a, hydrated_profile_state::Empty> {
        HydratedProfileBuilder::new()
    }
}

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

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `acceptingSupporters` field (optional)
    pub fn accepting_supporters(mut self, value: impl Into<Option<bool>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `acceptingSupporters` field to an Option value (optional)
    pub fn maybe_accepting_supporters(mut self, value: Option<bool>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `avatar` field (optional)
    pub fn avatar(mut self, value: impl Into<Option<BlobRef<'a>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `avatar` field to an Option value (optional)
    pub fn maybe_avatar(mut self, value: Option<BlobRef<'a>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `banner` field (optional)
    pub fn banner(mut self, value: impl Into<Option<BlobRef<'a>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `banner` field to an Option value (optional)
    pub fn maybe_banner(mut self, value: Option<BlobRef<'a>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `description` field (optional)
    pub fn description(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `description` field to an Option value (optional)
    pub fn maybe_description(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<'a, S> HydratedProfileBuilder<'a, S>
where
    S: hydrated_profile_state::State,
    S::Did: hydrated_profile_state::IsUnset,
{
    /// Set the `did` field (required)
    pub fn did(
        mut self,
        value: impl Into<Did<'a>>,
    ) -> HydratedProfileBuilder<'a, hydrated_profile_state::SetDid<S>> {
        self._fields.4 = Option::Some(value.into());
        HydratedProfileBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `displayName` field (optional)
    pub fn display_name(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.5 = value.into();
        self
    }
    /// Set the `displayName` field to an Option value (optional)
    pub fn maybe_display_name(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.5 = value;
        self
    }
}

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `facets` field (optional)
    pub fn facets(mut self, value: impl Into<Option<Vec<Facet<'a>>>>) -> Self {
        self._fields.6 = value.into();
        self
    }
    /// Set the `facets` field to an Option value (optional)
    pub fn maybe_facets(mut self, value: Option<Vec<Facet<'a>>>) -> Self {
        self._fields.6 = value;
        self
    }
}

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `handle` field (optional)
    pub fn handle(mut self, value: impl Into<Option<Handle<'a>>>) -> Self {
        self._fields.7 = value.into();
        self
    }
    /// Set the `handle` field to an Option value (optional)
    pub fn maybe_handle(mut self, value: Option<Handle<'a>>) -> Self {
        self._fields.7 = value;
        self
    }
}

impl<'a, S: hydrated_profile_state::State> HydratedProfileBuilder<'a, S> {
    /// Set the `supporterCount` field (optional)
    pub fn supporter_count(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.8 = value.into();
        self
    }
    /// Set the `supporterCount` field to an Option value (optional)
    pub fn maybe_supporter_count(mut self, value: Option<i64>) -> Self {
        self._fields.8 = value;
        self
    }
}

impl<'a, S> HydratedProfileBuilder<'a, S>
where
    S: hydrated_profile_state::State,
    S::Did: hydrated_profile_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> HydratedProfile<'a> {
        HydratedProfile {
            accepting_supporters: self._fields.0,
            avatar: self._fields.1,
            banner: self._fields.2,
            description: self._fields.3,
            did: self._fields.4.unwrap(),
            display_name: self._fields.5,
            facets: self._fields.6,
            handle: self._fields.7,
            supporter_count: self._fields.8,
            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>,
        >,
    ) -> HydratedProfile<'a> {
        HydratedProfile {
            accepting_supporters: self._fields.0,
            avatar: self._fields.1,
            banner: self._fields.2,
            description: self._fields.3,
            did: self._fields.4.unwrap(),
            display_name: self._fields.5,
            facets: self._fields.6,
            handle: self._fields.7,
            supporter_count: self._fields.8,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_com_atprotofans_hydratedProfile() -> 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("com.atprotofans.hydratedProfile"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "A hydrated identity profile with computed fields.",
                        ),
                    ),
                    required: Some(vec![SmolStr::new_static("did")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("acceptingSupporters"),
                            LexObjectProperty::Boolean(LexBoolean {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("avatar"),
                            LexObjectProperty::Blob(LexBlob { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("banner"),
                            LexObjectProperty::Blob(LexBlob { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("description"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "Profile bio/description with optional rich text.",
                                    ),
                                ),
                                max_length: Some(2560usize),
                                max_graphemes: Some(256usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("did"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("DID of the profile owner."),
                                ),
                                format: Some(LexStringFormat::Did),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("displayName"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("Display name for the profile."),
                                ),
                                max_length: Some(640usize),
                                max_graphemes: Some(64usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("facets"),
                            LexObjectProperty::Array(LexArray {
                                description: Some(
                                    CowStr::new_static(
                                        "Rich text facets for description annotations.",
                                    ),
                                ),
                                items: LexArrayItem::Ref(LexRef {
                                    r#ref: CowStr::new_static("app.bsky.richtext.facet"),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("handle"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("Handle of the profile owner."),
                                ),
                                format: Some(LexStringFormat::Handle),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("supporterCount"),
                            LexObjectProperty::Integer(LexInteger {
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}