// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: games.gamesgamesgamesgames.feed.listItem
//
// 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};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "games.gamesgamesgamesgames.feed.listItem",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct ListItem<S: BosStr = DefaultStr> {
    pub added_at: Datetime,
    pub game_uri: AtUri<S>,
    pub list_uri: AtUri<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 ListItemGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: ListItem<S>,
}

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

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

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

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

impl<S: BosStr> Collection for ListItem<S> {
    const NSID: &'static str = "games.gamesgamesgamesgames.feed.listItem";
    type Record = ListItemRecord;
}

impl Collection for ListItemRecord {
    const NSID: &'static str = "games.gamesgamesgamesgames.feed.listItem";
    type Record = ListItemRecord;
}

impl<S: BosStr> LexiconSchema for ListItem<S> {
    fn nsid() -> &'static str {
        "games.gamesgamesgamesgames.feed.listItem"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_games_gamesgamesgamesgames_feed_listItem()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

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

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

impl<S: BosStr> ListItem<S> {
    /// Create a new builder for this type
    pub fn builder() -> ListItemBuilder<list_item_state::Empty, S> {
        ListItemBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> ListItemBuilder<St, S>
where
    St: list_item_state::State,
    St::AddedAt: list_item_state::IsUnset,
{
    /// Set the `addedAt` field (required)
    pub fn added_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> ListItemBuilder<list_item_state::SetAddedAt<St>, S> {
        self._fields.0 = Option::Some(value.into());
        ListItemBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ListItemBuilder<St, S>
where
    St: list_item_state::State,
    St::GameUri: list_item_state::IsUnset,
{
    /// Set the `gameUri` field (required)
    pub fn game_uri(
        mut self,
        value: impl Into<AtUri<S>>,
    ) -> ListItemBuilder<list_item_state::SetGameUri<St>, S> {
        self._fields.1 = Option::Some(value.into());
        ListItemBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ListItemBuilder<St, S>
where
    St: list_item_state::State,
    St::ListUri: list_item_state::IsUnset,
{
    /// Set the `listUri` field (required)
    pub fn list_uri(
        mut self,
        value: impl Into<AtUri<S>>,
    ) -> ListItemBuilder<list_item_state::SetListUri<St>, S> {
        self._fields.2 = Option::Some(value.into());
        ListItemBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ListItemBuilder<St, S>
where
    St: list_item_state::State,
    St::AddedAt: list_item_state::IsSet,
    St::GameUri: list_item_state::IsSet,
    St::ListUri: list_item_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> ListItem<S> {
        ListItem {
            added_at: self._fields.0.unwrap(),
            game_uri: self._fields.1.unwrap(),
            list_uri: self._fields.2.unwrap(),
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> ListItem<S> {
        ListItem {
            added_at: self._fields.0.unwrap(),
            game_uri: self._fields.1.unwrap(),
            list_uri: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_games_gamesgamesgamesgames_feed_listItem() -> 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("games.gamesgamesgamesgames.feed.listItem"),
        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("listUri"),
                                SmolStr::new_static("gameUri"),
                                SmolStr::new_static("addedAt")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("addedAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("gameUri"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::AtUri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("listUri"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::AtUri),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}