// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: org.okazu-diary.material.external
//
// 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, UriValue};
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::com_atproto::label::SelfLabels;
use crate::com_atproto::repo::strong_ref::StrongRef;
use crate::org_okazu_diary::material::Tag;
use crate::org_okazu_diary::material::external;
/// A descriptor of a pornographic material.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct External<'a> {
    ///Description of the material, typically taken from the material's HTML metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<CowStr<'a>>,
    ///General-purpose self-label values primarily for consumption by generic AT clients who may not expect the sensitive nature of this Lexicon. Publishers of this record are strongly recommended to always include at least one of the protocol-global label values; namely, `porn`, `sexual`, `graphic-media`, and `nudity`, unless the material and tags are all known to be safe by themselves. It is the safest to unconditionally include the `sexual` value, which has the `adultOnly` semantics, but it is acceptable to use a stronger (`porn`/`graphic-media`) or more moderate (`nudity`) value instead if the user decides so.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub generic_labels: Option<SelfLabels<'a>>,
    ///User-specified self-label values for the material. While the Lexicon by its nature assumes the material to be possibly sensitive by default, these label values are intended to signal that a warning should be put on the material even for the explicit Okazu-Diary.org application users who are willing to see mature contents in general.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub labels: Option<SelfLabels<'a>>,
    ///Reference to a record representing the material, such as a Bluesky post.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub record: Option<StrongRef<'a>>,
    ///User-specified tags for the material.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub tags: Option<Vec<Tag<'a>>>,
    ///Link to a thumbnail of the material, typically taken from the material's HTML metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub thumb: Option<external::Thumb<'a>>,
    ///Title of the material, typically taken from the material's HTML `<title>` element.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub title: Option<CowStr<'a>>,
    ///URI of the material, typically an HTTP(S) URL. If the URL is of a Web interface for an AT resource (such as a `bsky.app` URL), the record should also include the `record` property referencing the AT resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub uri: Option<UriValue<'a>>,
    ///Reference to an `org.okazu-diary.feed.material` record from another repository from which this collection item is derived. Clients can use this property to offer the user to update this record if the original record have updated from the referenced version.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub via: Option<StrongRef<'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 ExternalGetRecordOutput<'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: External<'a>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Thumb<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
    #[serde(borrow)]
    pub url: UriValue<'a>,
}

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct ExternalRecord;
impl XrpcResp for ExternalRecord {
    const NSID: &'static str = "org.okazu-diary.material.external";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = ExternalGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

impl Collection for External<'_> {
    const NSID: &'static str = "org.okazu-diary.material.external";
    type Record = ExternalRecord;
}

impl Collection for ExternalRecord {
    const NSID: &'static str = "org.okazu-diary.material.external";
    type Record = ExternalRecord;
}

impl<'a> LexiconSchema for External<'a> {
    fn nsid() -> &'static str {
        "org.okazu-diary.material.external"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_org_okazu_diary_material_external()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.tags {
            #[allow(unused_comparisons)]
            if value.len() > 64usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("tags"),
                    max: 64usize,
                    actual: value.len(),
                });
            }
        }
        Ok(())
    }
}

impl<'a> LexiconSchema for Thumb<'a> {
    fn nsid() -> &'static str {
        "org.okazu-diary.material.external"
    }
    fn def_name() -> &'static str {
        "thumb"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_org_okazu_diary_material_external()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type
pub struct ExternalBuilder<'a, S: external_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<CowStr<'a>>,
        Option<SelfLabels<'a>>,
        Option<SelfLabels<'a>>,
        Option<StrongRef<'a>>,
        Option<Vec<Tag<'a>>>,
        Option<external::Thumb<'a>>,
        Option<CowStr<'a>>,
        Option<UriValue<'a>>,
        Option<StrongRef<'a>>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> External<'a> {
    /// Create a new builder for this type
    pub fn new() -> ExternalBuilder<'a, external_state::Empty> {
        ExternalBuilder::new()
    }
}

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

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `description` field (optional)
    pub fn description(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.0 = 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.0 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `genericLabels` field (optional)
    pub fn generic_labels(mut self, value: impl Into<Option<SelfLabels<'a>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `genericLabels` field to an Option value (optional)
    pub fn maybe_generic_labels(mut self, value: Option<SelfLabels<'a>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `labels` field (optional)
    pub fn labels(mut self, value: impl Into<Option<SelfLabels<'a>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `labels` field to an Option value (optional)
    pub fn maybe_labels(mut self, value: Option<SelfLabels<'a>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `record` field (optional)
    pub fn record(mut self, value: impl Into<Option<StrongRef<'a>>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `record` field to an Option value (optional)
    pub fn maybe_record(mut self, value: Option<StrongRef<'a>>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `tags` field (optional)
    pub fn tags(mut self, value: impl Into<Option<Vec<Tag<'a>>>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `tags` field to an Option value (optional)
    pub fn maybe_tags(mut self, value: Option<Vec<Tag<'a>>>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `thumb` field (optional)
    pub fn thumb(mut self, value: impl Into<Option<external::Thumb<'a>>>) -> Self {
        self._fields.5 = value.into();
        self
    }
    /// Set the `thumb` field to an Option value (optional)
    pub fn maybe_thumb(mut self, value: Option<external::Thumb<'a>>) -> Self {
        self._fields.5 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `title` field (optional)
    pub fn title(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.6 = value.into();
        self
    }
    /// Set the `title` field to an Option value (optional)
    pub fn maybe_title(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.6 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `uri` field (optional)
    pub fn uri(mut self, value: impl Into<Option<UriValue<'a>>>) -> Self {
        self._fields.7 = value.into();
        self
    }
    /// Set the `uri` field to an Option value (optional)
    pub fn maybe_uri(mut self, value: Option<UriValue<'a>>) -> Self {
        self._fields.7 = value;
        self
    }
}

impl<'a, S: external_state::State> ExternalBuilder<'a, S> {
    /// Set the `via` field (optional)
    pub fn via(mut self, value: impl Into<Option<StrongRef<'a>>>) -> Self {
        self._fields.8 = value.into();
        self
    }
    /// Set the `via` field to an Option value (optional)
    pub fn maybe_via(mut self, value: Option<StrongRef<'a>>) -> Self {
        self._fields.8 = value;
        self
    }
}

impl<'a, S> ExternalBuilder<'a, S>
where
    S: external_state::State,
{
    /// Build the final struct
    pub fn build(self) -> External<'a> {
        External {
            description: self._fields.0,
            generic_labels: self._fields.1,
            labels: self._fields.2,
            record: self._fields.3,
            tags: self._fields.4,
            thumb: self._fields.5,
            title: self._fields.6,
            uri: self._fields.7,
            via: 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>,
        >,
    ) -> External<'a> {
        External {
            description: self._fields.0,
            generic_labels: self._fields.1,
            labels: self._fields.2,
            record: self._fields.3,
            tags: self._fields.4,
            thumb: self._fields.5,
            title: self._fields.6,
            uri: self._fields.7,
            via: self._fields.8,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_org_okazu_diary_material_external() -> 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("org.okazu-diary.material.external"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static("A descriptor of a pornographic material."),
                    ),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("description"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Description of the material, typically taken from the material's HTML metadata.",
                                        ),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("genericLabels"),
                                LexObjectProperty::Union(LexRefUnion {
                                    description: Some(
                                        CowStr::new_static(
                                            "General-purpose self-label values primarily for consumption by generic AT clients who may not expect the sensitive nature of this Lexicon. Publishers of this record are strongly recommended to always include at least one of the protocol-global label values; namely, `porn`, `sexual`, `graphic-media`, and `nudity`, unless the material and tags are all known to be safe by themselves. It is the safest to unconditionally include the `sexual` value, which has the `adultOnly` semantics, but it is acceptable to use a stronger (`porn`/`graphic-media`) or more moderate (`nudity`) value instead if the user decides so.",
                                        ),
                                    ),
                                    refs: vec![
                                        CowStr::new_static("com.atproto.label.defs#selfLabels")
                                    ],
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("labels"),
                                LexObjectProperty::Union(LexRefUnion {
                                    description: Some(
                                        CowStr::new_static(
                                            "User-specified self-label values for the material. While the Lexicon by its nature assumes the material to be possibly sensitive by default, these label values are intended to signal that a warning should be put on the material even for the explicit Okazu-Diary.org application users who are willing to see mature contents in general.",
                                        ),
                                    ),
                                    refs: vec![
                                        CowStr::new_static("com.atproto.label.defs#selfLabels")
                                    ],
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("record"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("com.atproto.repo.strongRef"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("tags"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static("User-specified tags for the material."),
                                    ),
                                    items: LexArrayItem::Ref(LexRef {
                                        r#ref: CowStr::new_static(
                                            "org.okazu-diary.material.defs#tag",
                                        ),
                                        ..Default::default()
                                    }),
                                    max_length: Some(64usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("thumb"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("#thumb"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("title"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Title of the material, typically taken from the material's HTML `<title>` element.",
                                        ),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("uri"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "URI of the material, typically an HTTP(S) URL. If the URL is of a Web interface for an AT resource (such as a `bsky.app` URL), the record should also include the `record` property referencing the AT resource.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Uri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("via"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("com.atproto.repo.strongRef"),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("thumb"),
                LexUserType::Object(LexObject {
                    required: Some(vec![SmolStr::new_static("url")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("cid"),
                            LexObjectProperty::String(LexString {
                                format: Some(LexStringFormat::Cid),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("url"),
                            LexObjectProperty::String(LexString {
                                format: Some(LexStringFormat::Uri),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

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

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

impl<'a> Thumb<'a> {
    /// Create a new builder for this type
    pub fn new() -> ThumbBuilder<'a, thumb_state::Empty> {
        ThumbBuilder::new()
    }
}

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

impl<'a, S: thumb_state::State> ThumbBuilder<'a, S> {
    /// Set the `cid` field (optional)
    pub fn cid(mut self, value: impl Into<Option<Cid<'a>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `cid` field to an Option value (optional)
    pub fn maybe_cid(mut self, value: Option<Cid<'a>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S> ThumbBuilder<'a, S>
where
    S: thumb_state::State,
    S::Url: thumb_state::IsUnset,
{
    /// Set the `url` field (required)
    pub fn url(
        mut self,
        value: impl Into<UriValue<'a>>,
    ) -> ThumbBuilder<'a, thumb_state::SetUrl<S>> {
        self._fields.1 = Option::Some(value.into());
        ThumbBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ThumbBuilder<'a, S>
where
    S: thumb_state::State,
    S::Url: thumb_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Thumb<'a> {
        Thumb {
            cid: self._fields.0,
            url: 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>,
        >,
    ) -> Thumb<'a> {
        Thumb {
            cid: self._fields.0,
            url: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}