// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: sh.weaver.notebook.authors
//
// 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::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid, Datetime};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::types::value::Data;
use jacquard_common::xrpc::XrpcResp;
use jacquard_derive::{IntoStatic, lexicon, open_union};
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::actor::ProfileViewBasic;
use crate::sh_weaver::actor::ProfileView;
use crate::sh_weaver::notebook::authors;
/// A single author in a Weaver notebook.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct AuthorListItem<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub profile: Option<AuthorListItemProfile<S>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[open_union]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(tag = "$type", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub enum AuthorListItemProfile<S: BosStr = DefaultStr> {
    #[serde(rename = "app.bsky.actor.defs#profileViewBasic")]
    ProfileViewBasic(Box<ProfileViewBasic<S>>),
    #[serde(rename = "sh.weaver.actor.defs#profileView")]
    ProfileView(Box<ProfileView<S>>),
}

/// Authors of a Weaver notebook.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "sh.weaver.notebook.authors",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Authors<S: BosStr = DefaultStr> {
    pub author_list: Vec<authors::AuthorListItem<S>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<Datetime>,
    #[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 AuthorsGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Authors<S>,
}

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

impl<S: BosStr> LexiconSchema for AuthorListItem<S> {
    fn nsid() -> &'static str {
        "sh.weaver.notebook.authors"
    }
    fn def_name() -> &'static str {
        "authorListItem"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_sh_weaver_notebook_authors()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

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

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

impl<S: BosStr> Collection for Authors<S> {
    const NSID: &'static str = "sh.weaver.notebook.authors";
    type Record = AuthorsRecord;
}

impl Collection for AuthorsRecord {
    const NSID: &'static str = "sh.weaver.notebook.authors";
    type Record = AuthorsRecord;
}

impl<S: BosStr> LexiconSchema for Authors<S> {
    fn nsid() -> &'static str {
        "sh.weaver.notebook.authors"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_sh_weaver_notebook_authors()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type.
pub struct AuthorListItemBuilder<
    St: author_list_item_state::State,
    S: BosStr = DefaultStr,
> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<i64>, Option<AuthorListItemProfile<S>>),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> AuthorListItem<S> {
    /// Create a new builder for this type
    pub fn builder() -> AuthorListItemBuilder<author_list_item_state::Empty, S> {
        AuthorListItemBuilder::builder()
    }
}

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

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

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

impl<St: author_list_item_state::State, S: BosStr> AuthorListItemBuilder<St, S> {
    /// Set the `profile` field (optional)
    pub fn profile(
        mut self,
        value: impl Into<Option<AuthorListItemProfile<S>>>,
    ) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `profile` field to an Option value (optional)
    pub fn maybe_profile(mut self, value: Option<AuthorListItemProfile<S>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> AuthorListItemBuilder<St, S>
where
    St: author_list_item_state::State,
    St::ProfileIndex: author_list_item_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> AuthorListItem<S> {
        AuthorListItem {
            index: self._fields.0,
            profile: self._fields.1,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<SmolStr, Data<S>>,
    ) -> AuthorListItem<S> {
        AuthorListItem {
            index: self._fields.0,
            profile: self._fields.1,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_sh_weaver_notebook_authors() -> 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("sh.weaver.notebook.authors"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("authorListItem"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static("A single author in a Weaver notebook."),
                    ),
                    required: Some(vec![SmolStr::new_static("profile, index")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("index"),
                            LexObjectProperty::Integer(LexInteger {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("profile"),
                            LexObjectProperty::Union(LexRefUnion {
                                refs: vec![
                                    CowStr::new_static("app.bsky.actor.defs#profileViewBasic"),
                                    CowStr::new_static("sh.weaver.actor.defs#profileView")
                                ],
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static("Authors of a Weaver notebook."),
                    ),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(vec![SmolStr::new_static("authorList")]),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("authorList"),
                                LexObjectProperty::Array(LexArray {
                                    items: LexArrayItem::Ref(LexRef {
                                        r#ref: CowStr::new_static("#authorListItem"),
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

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

/// Builder for constructing an instance of this type.
pub struct AuthorsBuilder<St: authors_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<Vec<authors::AuthorListItem<S>>>, Option<Datetime>),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> Authors<S> {
    /// Create a new builder for this type
    pub fn builder() -> AuthorsBuilder<authors_state::Empty, S> {
        AuthorsBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> AuthorsBuilder<St, S>
where
    St: authors_state::State,
    St::AuthorList: authors_state::IsUnset,
{
    /// Set the `authorList` field (required)
    pub fn author_list(
        mut self,
        value: impl Into<Vec<authors::AuthorListItem<S>>>,
    ) -> AuthorsBuilder<authors_state::SetAuthorList<St>, S> {
        self._fields.0 = Option::Some(value.into());
        AuthorsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: authors_state::State, S: BosStr> AuthorsBuilder<St, S> {
    /// Set the `createdAt` field (optional)
    pub fn created_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `createdAt` field to an Option value (optional)
    pub fn maybe_created_at(mut self, value: Option<Datetime>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> AuthorsBuilder<St, S>
where
    St: authors_state::State,
    St::AuthorList: authors_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Authors<S> {
        Authors {
            author_list: self._fields.0.unwrap(),
            created_at: self._fields.1,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Authors<S> {
        Authors {
            author_list: self._fields.0.unwrap(),
            created_at: self._fields.1,
            extra_data: Some(extra_data),
        }
    }
}