// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: blog.pckt.blog
//
// 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};

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::blob::BlobRef;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid, Datetime, UriValue};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::types::value::Data;
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::blog_pckt::blog;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "blog.pckt.blog",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Blog<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<BlobRef<S>>,
    pub name: S,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rss: Option<UriValue<S>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub theme: Option<blog::Theme<S>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<Datetime>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<UriValue<S>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// 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 BlogGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Blog<S>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Palette<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accent: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub link: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub surface_hover: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<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, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Theme<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dark: Option<blog::Palette<S>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub light: Option<blog::Palette<S>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

impl<S: BosStr> Blog<S> {
    pub fn uri(uri: S) -> Result<RecordUri<S, BlogRecord>, UriError> {
        RecordUri::try_from_uri(AtUri::new(uri)?)
    }
}

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

#[derive(Debug, Serialize, Deserialize)]
pub struct BlogRecord;
impl XrpcResp for BlogRecord {
    const NSID: &'static str = "blog.pckt.blog";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = BlogGetRecordOutput<S>;
    type Err = RecordError;
}

impl<S: BosStr> From<BlogGetRecordOutput<S>> for Blog<S> {
    fn from(output: BlogGetRecordOutput<S>) -> Self {
        output.value
    }
}

impl<S: BosStr> Collection for Blog<S> {
    const NSID: &'static str = "blog.pckt.blog";
    type Record = BlogRecord;
}

impl Collection for BlogRecord {
    const NSID: &'static str = "blog.pckt.blog";
    type Record = BlogRecord;
}

impl<S: BosStr> LexiconSchema for Blog<S> {
    fn nsid() -> &'static str {
        "blog.pckt.blog"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_blog_pckt_blog()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.icon {
            {
                let mime = value.blob().mime_type.as_str();
                let accepted: &[&str] = &["image/*"];
                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("icon"),
                        accepted: vec!["image/*".to_string()],
                        actual: mime.to_string(),
                    });
                }
            }
        }
        Ok(())
    }
}

impl<S: BosStr> LexiconSchema for Palette<S> {
    fn nsid() -> &'static str {
        "blog.pckt.blog"
    }
    fn def_name() -> &'static str {
        "palette"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_blog_pckt_blog()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

impl<S: BosStr> LexiconSchema for Theme<S> {
    fn nsid() -> &'static str {
        "blog.pckt.blog"
    }
    fn def_name() -> &'static str {
        "theme"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_blog_pckt_blog()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type.
pub struct BlogBuilder<St: blog_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (
        Option<S>,
        Option<BlobRef<S>>,
        Option<S>,
        Option<UriValue<S>>,
        Option<blog::Theme<S>>,
        Option<Datetime>,
        Option<UriValue<S>>,
    ),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> Blog<S> {
    /// Create a new builder for this type
    pub fn builder() -> BlogBuilder<blog_state::Empty, S> {
        BlogBuilder::builder()
    }
}

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

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

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

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

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

impl<St: blog_state::State, S: BosStr> BlogBuilder<St, S> {
    /// Set the `rss` field (optional)
    pub fn rss(mut self, value: impl Into<Option<UriValue<S>>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `rss` field to an Option value (optional)
    pub fn maybe_rss(mut self, value: Option<UriValue<S>>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<St: blog_state::State, S: BosStr> BlogBuilder<St, S> {
    /// Set the `theme` field (optional)
    pub fn theme(mut self, value: impl Into<Option<blog::Theme<S>>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `theme` field to an Option value (optional)
    pub fn maybe_theme(mut self, value: Option<blog::Theme<S>>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<St: blog_state::State, S: BosStr> BlogBuilder<St, S> {
    /// Set the `updatedAt` field (optional)
    pub fn updated_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.5 = 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.5 = value;
        self
    }
}

impl<St: blog_state::State, S: BosStr> BlogBuilder<St, S> {
    /// Set the `url` field (optional)
    pub fn url(mut self, value: impl Into<Option<UriValue<S>>>) -> Self {
        self._fields.6 = value.into();
        self
    }
    /// Set the `url` field to an Option value (optional)
    pub fn maybe_url(mut self, value: Option<UriValue<S>>) -> Self {
        self._fields.6 = value;
        self
    }
}

impl<St, S: BosStr> BlogBuilder<St, S>
where
    St: blog_state::State,
    St::Name: blog_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Blog<S> {
        Blog {
            description: self._fields.0,
            icon: self._fields.1,
            name: self._fields.2.unwrap(),
            rss: self._fields.3,
            theme: self._fields.4,
            updated_at: self._fields.5,
            url: self._fields.6,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Blog<S> {
        Blog {
            description: self._fields.0,
            icon: self._fields.1,
            name: self._fields.2.unwrap(),
            rss: self._fields.3,
            theme: self._fields.4,
            updated_at: self._fields.5,
            url: self._fields.6,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_blog_pckt_blog() -> 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("blog.pckt.blog"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(vec![SmolStr::new_static("name")]),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("description"),
                                LexObjectProperty::String(LexString {
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("icon"),
                                LexObjectProperty::Blob(LexBlob { ..Default::default() }),
                            );
                            map.insert(
                                SmolStr::new_static("name"),
                                LexObjectProperty::String(LexString {
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("rss"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Uri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("theme"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("#theme"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("updatedAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("url"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Uri),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("palette"),
                LexUserType::Object(LexObject {
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("accent"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("background"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("link"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("surfaceHover"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("text"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("theme"),
                LexUserType::Object(LexObject {
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("dark"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static("#palette"),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("font"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("light"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static("#palette"),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}