// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: garden.lexicon.documentation
//
// 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, Nsid, Cid, Datetime, Language};
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::garden_lexicon::documentation;
/// Documentation for a definition within a lexicon.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct DefinitionDoc<'a> {
    ///Localized descriptions for this definition.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<Vec<documentation::LocalizedString<'a>>>,
    ///The name of the definition being documented (e.g., 'main', 'replyRef').
    #[serde(borrow)]
    pub name: CowStr<'a>,
    ///Documentation for properties within this definition.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub properties: Option<Vec<documentation::PropertyDoc<'a>>>,
}

/// A string with an associated language code.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct LocalizedString<'a> {
    ///ISO 639 language code (e.g., 'en', 'es', 'ja').
    pub lang: Language,
    ///The localized string value.
    #[serde(borrow)]
    pub value: CowStr<'a>,
}

/// Auxiliary documentation for a lexicon schema, supporting localized descriptions for the lexicon and its properties.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Documentation<'a> {
    ///Timestamp when this documentation was created.
    pub created_at: Datetime,
    ///Documentation for specific definitions within the lexicon (e.g., main, replyRef).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub definitions: Option<Vec<documentation::DefinitionDoc<'a>>>,
    ///Localized descriptions for the lexicon.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<Vec<documentation::LocalizedString<'a>>>,
    ///The NSID of the lexicon being documented.
    #[serde(borrow)]
    pub lexicon: Nsid<'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 DocumentationGetRecordOutput<'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: Documentation<'a>,
}

/// Documentation for a specific property within a definition.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct PropertyDoc<'a> {
    ///Localized descriptions for this property.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<Vec<documentation::LocalizedString<'a>>>,
    ///The property name being documented.
    #[serde(borrow)]
    pub name: CowStr<'a>,
}

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

impl<'a> LexiconSchema for DefinitionDoc<'a> {
    fn nsid() -> &'static str {
        "garden.lexicon.documentation"
    }
    fn def_name() -> &'static str {
        "definitionDoc"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_garden_lexicon_documentation()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.name;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 512usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("name"),
                    max: 512usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

impl<'a> LexiconSchema for LocalizedString<'a> {
    fn nsid() -> &'static str {
        "garden.lexicon.documentation"
    }
    fn def_name() -> &'static str {
        "localizedString"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_garden_lexicon_documentation()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.value;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 10000usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("value"),
                    max: 10000usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

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

#[derive(Debug, Serialize, Deserialize)]
pub struct DocumentationRecord;
impl XrpcResp for DocumentationRecord {
    const NSID: &'static str = "garden.lexicon.documentation";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = DocumentationGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

impl Collection for Documentation<'_> {
    const NSID: &'static str = "garden.lexicon.documentation";
    type Record = DocumentationRecord;
}

impl Collection for DocumentationRecord {
    const NSID: &'static str = "garden.lexicon.documentation";
    type Record = DocumentationRecord;
}

impl<'a> LexiconSchema for Documentation<'a> {
    fn nsid() -> &'static str {
        "garden.lexicon.documentation"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_garden_lexicon_documentation()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

impl<'a> LexiconSchema for PropertyDoc<'a> {
    fn nsid() -> &'static str {
        "garden.lexicon.documentation"
    }
    fn def_name() -> &'static str {
        "propertyDoc"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_garden_lexicon_documentation()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.name;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 256usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("name"),
                    max: 256usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

fn lexicon_doc_garden_lexicon_documentation() -> 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("garden.lexicon.documentation"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("definitionDoc"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "Documentation for a definition within a lexicon.",
                        ),
                    ),
                    required: Some(vec![SmolStr::new_static("name")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("description"),
                            LexObjectProperty::Array(LexArray {
                                description: Some(
                                    CowStr::new_static(
                                        "Localized descriptions for this definition.",
                                    ),
                                ),
                                items: LexArrayItem::Ref(LexRef {
                                    r#ref: CowStr::new_static("#localizedString"),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("name"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "The name of the definition being documented (e.g., 'main', 'replyRef').",
                                    ),
                                ),
                                max_length: Some(512usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("properties"),
                            LexObjectProperty::Array(LexArray {
                                description: Some(
                                    CowStr::new_static(
                                        "Documentation for properties within this definition.",
                                    ),
                                ),
                                items: LexArrayItem::Ref(LexRef {
                                    r#ref: CowStr::new_static("#propertyDoc"),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("localizedString"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static("A string with an associated language code."),
                    ),
                    required: Some(
                        vec![SmolStr::new_static("lang"), SmolStr::new_static("value")],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("lang"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "ISO 639 language code (e.g., 'en', 'es', 'ja').",
                                    ),
                                ),
                                format: Some(LexStringFormat::Language),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("value"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("The localized string value."),
                                ),
                                max_length: Some(10000usize),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "Auxiliary documentation for a lexicon schema, supporting localized descriptions for the lexicon and its properties.",
                        ),
                    ),
                    key: Some(CowStr::new_static("nsid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("lexicon"),
                                SmolStr::new_static("createdAt")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Timestamp when this documentation was created.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("definitions"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static(
                                            "Documentation for specific definitions within the lexicon (e.g., main, replyRef).",
                                        ),
                                    ),
                                    items: LexArrayItem::Ref(LexRef {
                                        r#ref: CowStr::new_static("#definitionDoc"),
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("description"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static(
                                            "Localized descriptions for the lexicon.",
                                        ),
                                    ),
                                    items: LexArrayItem::Ref(LexRef {
                                        r#ref: CowStr::new_static("#localizedString"),
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("lexicon"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The NSID of the lexicon being documented.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Nsid),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("propertyDoc"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "Documentation for a specific property within a definition.",
                        ),
                    ),
                    required: Some(vec![SmolStr::new_static("name")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("description"),
                            LexObjectProperty::Array(LexArray {
                                description: Some(
                                    CowStr::new_static(
                                        "Localized descriptions for this property.",
                                    ),
                                ),
                                items: LexArrayItem::Ref(LexRef {
                                    r#ref: CowStr::new_static("#localizedString"),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("name"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("The property name being documented."),
                                ),
                                max_length: Some(256usize),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

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

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

impl<'a> LocalizedString<'a> {
    /// Create a new builder for this type
    pub fn new() -> LocalizedStringBuilder<'a, localized_string_state::Empty> {
        LocalizedStringBuilder::new()
    }
}

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

impl<'a, S> LocalizedStringBuilder<'a, S>
where
    S: localized_string_state::State,
    S::Lang: localized_string_state::IsUnset,
{
    /// Set the `lang` field (required)
    pub fn lang(
        mut self,
        value: impl Into<Language>,
    ) -> LocalizedStringBuilder<'a, localized_string_state::SetLang<S>> {
        self._fields.0 = Option::Some(value.into());
        LocalizedStringBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> LocalizedStringBuilder<'a, S>
where
    S: localized_string_state::State,
    S::Value: localized_string_state::IsUnset,
{
    /// Set the `value` field (required)
    pub fn value(
        mut self,
        value: impl Into<CowStr<'a>>,
    ) -> LocalizedStringBuilder<'a, localized_string_state::SetValue<S>> {
        self._fields.1 = Option::Some(value.into());
        LocalizedStringBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> LocalizedStringBuilder<'a, S>
where
    S: localized_string_state::State,
    S::Value: localized_string_state::IsSet,
    S::Lang: localized_string_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> LocalizedString<'a> {
        LocalizedString {
            lang: self._fields.0.unwrap(),
            value: 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>,
        >,
    ) -> LocalizedString<'a> {
        LocalizedString {
            lang: self._fields.0.unwrap(),
            value: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

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

/// Builder for constructing an instance of this type
pub struct DocumentationBuilder<'a, S: documentation_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<Datetime>,
        Option<Vec<documentation::DefinitionDoc<'a>>>,
        Option<Vec<documentation::LocalizedString<'a>>>,
        Option<Nsid<'a>>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Documentation<'a> {
    /// Create a new builder for this type
    pub fn new() -> DocumentationBuilder<'a, documentation_state::Empty> {
        DocumentationBuilder::new()
    }
}

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

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

impl<'a, S: documentation_state::State> DocumentationBuilder<'a, S> {
    /// Set the `definitions` field (optional)
    pub fn definitions(
        mut self,
        value: impl Into<Option<Vec<documentation::DefinitionDoc<'a>>>>,
    ) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `definitions` field to an Option value (optional)
    pub fn maybe_definitions(
        mut self,
        value: Option<Vec<documentation::DefinitionDoc<'a>>>,
    ) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S: documentation_state::State> DocumentationBuilder<'a, S> {
    /// Set the `description` field (optional)
    pub fn description(
        mut self,
        value: impl Into<Option<Vec<documentation::LocalizedString<'a>>>>,
    ) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `description` field to an Option value (optional)
    pub fn maybe_description(
        mut self,
        value: Option<Vec<documentation::LocalizedString<'a>>>,
    ) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<'a, S> DocumentationBuilder<'a, S>
where
    S: documentation_state::State,
    S::Lexicon: documentation_state::IsUnset,
{
    /// Set the `lexicon` field (required)
    pub fn lexicon(
        mut self,
        value: impl Into<Nsid<'a>>,
    ) -> DocumentationBuilder<'a, documentation_state::SetLexicon<S>> {
        self._fields.3 = Option::Some(value.into());
        DocumentationBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> DocumentationBuilder<'a, S>
where
    S: documentation_state::State,
    S::Lexicon: documentation_state::IsSet,
    S::CreatedAt: documentation_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Documentation<'a> {
        Documentation {
            created_at: self._fields.0.unwrap(),
            definitions: self._fields.1,
            description: self._fields.2,
            lexicon: self._fields.3.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>,
        >,
    ) -> Documentation<'a> {
        Documentation {
            created_at: self._fields.0.unwrap(),
            definitions: self._fields.1,
            description: self._fields.2,
            lexicon: self._fields.3.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}