// @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;

#[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};
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::repo::strong_ref::StrongRef;
use crate::games_gamesgamesgamesgames::CreditEntry;
/// A relationship between a game and a profile.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Credit<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub actor: Option<StrongRef<'a>>,
    ///The roles this profile played in the creation of the game.
    #[serde(borrow)]
    pub credits: Vec<CreditEntry<'a>>,
    ///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")]
    #[serde(borrow)]
    pub display_name: Option<CowStr<'a>>,
    ///The game to which this profile is being credited.
    #[serde(borrow)]
    pub game: 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 CreditGetRecordOutput<'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: Credit<'a>,
}

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

/// 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<'de> = CreditGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

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

/// Builder for constructing an instance of this type
pub struct CreditBuilder<'a, S: credit_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<StrongRef<'a>>,
        Option<Vec<CreditEntry<'a>>>,
        Option<CowStr<'a>>,
        Option<StrongRef<'a>>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

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

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

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

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

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

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

impl<'a, S> CreditBuilder<'a, S>
where
    S: credit_state::State,
    S::Game: credit_state::IsSet,
    S::Credits: credit_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Credit<'a> {
        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<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Credit<'a> {
        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()
    }
}