// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: games.gamesgamesgamesgames.actor.credit
//
// 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};
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::com_atproto::repo::strong_ref::StrongRef;
use crate::games_gamesgamesgamesgames::CreditEntry;
/// A relationship between a game and a profile.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "games.gamesgamesgamesgames.actor.credit",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Credit<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actor: Option<StrongRef<S>>,
    ///The roles this profile played in the creation of the game.
    pub credits: Vec<CreditEntry<S>>,
    ///The name to be used if there is no profile associated with this credit, or the profile is inaccessible.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<S>,
    ///The game to which this profile is being credited.
    pub game: StrongRef<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 CreditGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Credit<S>,
}

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

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

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

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

impl<S: BosStr> Collection for Credit<S> {
    const NSID: &'static str = "games.gamesgamesgamesgames.actor.credit";
    type Record = CreditRecord;
}

impl Collection for CreditRecord {
    const NSID: &'static str = "games.gamesgamesgamesgames.actor.credit";
    type Record = CreditRecord;
}

impl<S: BosStr> LexiconSchema for Credit<S> {
    fn nsid() -> &'static str {
        "games.gamesgamesgamesgames.actor.credit"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_games_gamesgamesgamesgames_actor_credit()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.display_name {
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 640usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("display_name"),
                    max: 640usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type.
pub struct CreditBuilder<St: credit_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (
        Option<StrongRef<S>>,
        Option<Vec<CreditEntry<S>>>,
        Option<S>,
        Option<StrongRef<S>>,
    ),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> Credit<S> {
    /// Create a new builder for this type
    pub fn builder() -> CreditBuilder<credit_state::Empty, S> {
        CreditBuilder::builder()
    }
}

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

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

impl<St: credit_state::State, S: BosStr> CreditBuilder<St, S> {
    /// Set the `actor` field (optional)
    pub fn actor(mut self, value: impl Into<Option<StrongRef<S>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `actor` field to an Option value (optional)
    pub fn maybe_actor(mut self, value: Option<StrongRef<S>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<St, S: BosStr> CreditBuilder<St, S>
where
    St: credit_state::State,
    St::Credits: credit_state::IsUnset,
{
    /// Set the `credits` field (required)
    pub fn credits(
        mut self,
        value: impl Into<Vec<CreditEntry<S>>>,
    ) -> CreditBuilder<credit_state::SetCredits<St>, S> {
        self._fields.1 = Option::Some(value.into());
        CreditBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: credit_state::State, S: BosStr> CreditBuilder<St, S> {
    /// Set the `displayName` field (optional)
    pub fn display_name(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `displayName` field to an Option value (optional)
    pub fn maybe_display_name(mut self, value: Option<S>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<St, S: BosStr> CreditBuilder<St, S>
where
    St: credit_state::State,
    St::Game: credit_state::IsUnset,
{
    /// Set the `game` field (required)
    pub fn game(
        mut self,
        value: impl Into<StrongRef<S>>,
    ) -> CreditBuilder<credit_state::SetGame<St>, S> {
        self._fields.3 = Option::Some(value.into());
        CreditBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> CreditBuilder<St, S>
where
    St: credit_state::State,
    St::Credits: credit_state::IsSet,
    St::Game: credit_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Credit<S> {
        Credit {
            actor: self._fields.0,
            credits: self._fields.1.unwrap(),
            display_name: self._fields.2,
            game: 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<SmolStr, Data<S>>) -> Credit<S> {
        Credit {
            actor: self._fields.0,
            credits: self._fields.1.unwrap(),
            display_name: self._fields.2,
            game: self._fields.3.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_games_gamesgamesgamesgames_actor_credit() -> 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.actor.credit"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "A relationship between a game and a profile.",
                        ),
                    ),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("game"), SmolStr::new_static("credits")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("actor"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("com.atproto.repo.strongRef"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("credits"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static(
                                            "The roles this profile played in the creation of the game.",
                                        ),
                                    ),
                                    items: LexArrayItem::Ref(LexRef {
                                        r#ref: CowStr::new_static(
                                            "games.gamesgamesgamesgames.defs#creditEntry",
                                        ),
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("displayName"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The name to be used if there is no profile associated with this credit, or the profile is inaccessible.",
                                        ),
                                    ),
                                    max_length: Some(640usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("game"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("com.atproto.repo.strongRef"),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}