// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: blue.2048.game
//
// 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};
use crate::blue__2048::SyncStatus;
/// A declaration of an instance of a at://2048 game

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "blue.2048.game",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Game<S: BosStr = DefaultStr> {
    ///The player no longer has any moves left  Defaults to `false`.
    #[serde(default = "_default_game_completed")]
    pub completed: bool,
    pub created_at: Datetime,
    ///The game's current score  Defaults to `0`.
    #[serde(default = "_default_game_current_score")]
    pub current_score: i64,
    ///This is the recording of the game. Like chess notation, but for 2048
    pub seeded_recording: S,
    ///The sync status of this record with the users AT Protocol repo.
    pub sync_status: SyncStatus<S>,
    ///The player has found a 2048 tile (they have won)  Defaults to `false`.
    #[serde(default = "_default_game_won")]
    pub won: bool,
    #[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 GameGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Game<S>,
}

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

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

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

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

impl<S: BosStr> Collection for Game<S> {
    const NSID: &'static str = "blue.2048.game";
    type Record = GameRecord;
}

impl Collection for GameRecord {
    const NSID: &'static str = "blue.2048.game";
    type Record = GameRecord;
}

impl<S: BosStr> LexiconSchema for Game<S> {
    fn nsid() -> &'static str {
        "blue.2048.game"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_blue_2048_game()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

fn _default_game_completed() -> bool {
    false
}

fn _default_game_current_score() -> i64 {
    0i64
}

fn _default_game_won() -> bool {
    false
}

pub mod game_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 Completed;
        type CreatedAt;
        type CurrentScore;
        type SeededRecording;
        type SyncStatus;
        type Won;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Completed = Unset;
        type CreatedAt = Unset;
        type CurrentScore = Unset;
        type SeededRecording = Unset;
        type SyncStatus = Unset;
        type Won = Unset;
    }
    ///State transition - sets the `completed` field to Set
    pub struct SetCompleted<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetCompleted<St> {}
    impl<St: State> State for SetCompleted<St> {
        type Completed = Set<members::completed>;
        type CreatedAt = St::CreatedAt;
        type CurrentScore = St::CurrentScore;
        type SeededRecording = St::SeededRecording;
        type SyncStatus = St::SyncStatus;
        type Won = St::Won;
    }
    ///State transition - sets the `created_at` field to Set
    pub struct SetCreatedAt<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetCreatedAt<St> {}
    impl<St: State> State for SetCreatedAt<St> {
        type Completed = St::Completed;
        type CreatedAt = Set<members::created_at>;
        type CurrentScore = St::CurrentScore;
        type SeededRecording = St::SeededRecording;
        type SyncStatus = St::SyncStatus;
        type Won = St::Won;
    }
    ///State transition - sets the `current_score` field to Set
    pub struct SetCurrentScore<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetCurrentScore<St> {}
    impl<St: State> State for SetCurrentScore<St> {
        type Completed = St::Completed;
        type CreatedAt = St::CreatedAt;
        type CurrentScore = Set<members::current_score>;
        type SeededRecording = St::SeededRecording;
        type SyncStatus = St::SyncStatus;
        type Won = St::Won;
    }
    ///State transition - sets the `seeded_recording` field to Set
    pub struct SetSeededRecording<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSeededRecording<St> {}
    impl<St: State> State for SetSeededRecording<St> {
        type Completed = St::Completed;
        type CreatedAt = St::CreatedAt;
        type CurrentScore = St::CurrentScore;
        type SeededRecording = Set<members::seeded_recording>;
        type SyncStatus = St::SyncStatus;
        type Won = St::Won;
    }
    ///State transition - sets the `sync_status` field to Set
    pub struct SetSyncStatus<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSyncStatus<St> {}
    impl<St: State> State for SetSyncStatus<St> {
        type Completed = St::Completed;
        type CreatedAt = St::CreatedAt;
        type CurrentScore = St::CurrentScore;
        type SeededRecording = St::SeededRecording;
        type SyncStatus = Set<members::sync_status>;
        type Won = St::Won;
    }
    ///State transition - sets the `won` field to Set
    pub struct SetWon<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetWon<St> {}
    impl<St: State> State for SetWon<St> {
        type Completed = St::Completed;
        type CreatedAt = St::CreatedAt;
        type CurrentScore = St::CurrentScore;
        type SeededRecording = St::SeededRecording;
        type SyncStatus = St::SyncStatus;
        type Won = Set<members::won>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `completed` field
        pub struct completed(());
        ///Marker type for the `created_at` field
        pub struct created_at(());
        ///Marker type for the `current_score` field
        pub struct current_score(());
        ///Marker type for the `seeded_recording` field
        pub struct seeded_recording(());
        ///Marker type for the `sync_status` field
        pub struct sync_status(());
        ///Marker type for the `won` field
        pub struct won(());
    }
}

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

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

impl<S: BosStr> Game<S> {
    /// Create a new builder for this type
    pub fn builder() -> GameBuilder<game_state::Empty, S> {
        GameBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> GameBuilder<St, S>
where
    St: game_state::State,
    St::Completed: game_state::IsUnset,
{
    /// Set the `completed` field (required)
    pub fn completed(
        mut self,
        value: impl Into<bool>,
    ) -> GameBuilder<game_state::SetCompleted<St>, S> {
        self._fields.0 = Option::Some(value.into());
        GameBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> GameBuilder<St, S>
where
    St: game_state::State,
    St::CreatedAt: game_state::IsUnset,
{
    /// Set the `createdAt` field (required)
    pub fn created_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> GameBuilder<game_state::SetCreatedAt<St>, S> {
        self._fields.1 = Option::Some(value.into());
        GameBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> GameBuilder<St, S>
where
    St: game_state::State,
    St::CurrentScore: game_state::IsUnset,
{
    /// Set the `currentScore` field (required)
    pub fn current_score(
        mut self,
        value: impl Into<i64>,
    ) -> GameBuilder<game_state::SetCurrentScore<St>, S> {
        self._fields.2 = Option::Some(value.into());
        GameBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> GameBuilder<St, S>
where
    St: game_state::State,
    St::SeededRecording: game_state::IsUnset,
{
    /// Set the `seededRecording` field (required)
    pub fn seeded_recording(
        mut self,
        value: impl Into<S>,
    ) -> GameBuilder<game_state::SetSeededRecording<St>, S> {
        self._fields.3 = Option::Some(value.into());
        GameBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> GameBuilder<St, S>
where
    St: game_state::State,
    St::SyncStatus: game_state::IsUnset,
{
    /// Set the `syncStatus` field (required)
    pub fn sync_status(
        mut self,
        value: impl Into<SyncStatus<S>>,
    ) -> GameBuilder<game_state::SetSyncStatus<St>, S> {
        self._fields.4 = Option::Some(value.into());
        GameBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> GameBuilder<St, S>
where
    St: game_state::State,
    St::Won: game_state::IsUnset,
{
    /// Set the `won` field (required)
    pub fn won(
        mut self,
        value: impl Into<bool>,
    ) -> GameBuilder<game_state::SetWon<St>, S> {
        self._fields.5 = Option::Some(value.into());
        GameBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> GameBuilder<St, S>
where
    St: game_state::State,
    St::Completed: game_state::IsSet,
    St::CreatedAt: game_state::IsSet,
    St::CurrentScore: game_state::IsSet,
    St::SeededRecording: game_state::IsSet,
    St::SyncStatus: game_state::IsSet,
    St::Won: game_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Game<S> {
        Game {
            completed: self._fields.0.unwrap(),
            created_at: self._fields.1.unwrap(),
            current_score: self._fields.2.unwrap(),
            seeded_recording: self._fields.3.unwrap(),
            sync_status: self._fields.4.unwrap(),
            won: self._fields.5.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>>) -> Game<S> {
        Game {
            completed: self._fields.0.unwrap(),
            created_at: self._fields.1.unwrap(),
            current_score: self._fields.2.unwrap(),
            seeded_recording: self._fields.3.unwrap(),
            sync_status: self._fields.4.unwrap(),
            won: self._fields.5.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_blue_2048_game() -> 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("blue.2048.game"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "A declaration of an instance of a at://2048 game",
                        ),
                    ),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("currentScore"),
                                SmolStr::new_static("won"),
                                SmolStr::new_static("completed"),
                                SmolStr::new_static("seededRecording"),
                                SmolStr::new_static("syncStatus"),
                                SmolStr::new_static("createdAt")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("completed"),
                                LexObjectProperty::Boolean(LexBoolean {
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("currentScore"),
                                LexObjectProperty::Integer(LexInteger {
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("seededRecording"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "This is the recording of the game. Like chess notation, but for 2048",
                                        ),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("syncStatus"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("blue.2048.defs#syncStatus"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("won"),
                                LexObjectProperty::Boolean(LexBoolean {
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}