// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: blue.atplay.score.defs
//
// 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};
use jacquard_common::deps::bytes::Bytes;

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::string::Datetime;
use jacquard_common::types::value::Data;
use jacquard_derive::IntoStatic;
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
/// Attestation signature proving a score was submitted through ATPlay SDK

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Attestation<S: BosStr = DefaultStr> {
    ///Timestamp when the attestation was created (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attested_at: Option<Datetime>,
    ///DID key reference for verifying the signature
    pub key: S,
    ///ES256 signature over the score record CID
    #[serde(with = "jacquard_common::serde_bytes_helper")]
    pub signature: Bytes,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

impl<S: BosStr> LexiconSchema for Attestation<S> {
    fn nsid() -> &'static str {
        "blue.atplay.score.defs"
    }
    fn def_name() -> &'static str {
        "attestation"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_blue_atplay_score_defs()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.key;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 512usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("key"),
                    max: 512usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

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

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

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

impl<S: BosStr> Attestation<S> {
    /// Create a new builder for this type
    pub fn builder() -> AttestationBuilder<attestation_state::Empty, S> {
        AttestationBuilder::builder()
    }
}

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

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

impl<St: attestation_state::State, S: BosStr> AttestationBuilder<St, S> {
    /// Set the `attestedAt` field (optional)
    pub fn attested_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `attestedAt` field to an Option value (optional)
    pub fn maybe_attested_at(mut self, value: Option<Datetime>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<St, S: BosStr> AttestationBuilder<St, S>
where
    St: attestation_state::State,
    St::Key: attestation_state::IsUnset,
{
    /// Set the `key` field (required)
    pub fn key(
        mut self,
        value: impl Into<S>,
    ) -> AttestationBuilder<attestation_state::SetKey<St>, S> {
        self._fields.1 = Option::Some(value.into());
        AttestationBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> AttestationBuilder<St, S>
where
    St: attestation_state::State,
    St::Signature: attestation_state::IsUnset,
{
    /// Set the `signature` field (required)
    pub fn signature(
        mut self,
        value: impl Into<Bytes>,
    ) -> AttestationBuilder<attestation_state::SetSignature<St>, S> {
        self._fields.2 = Option::Some(value.into());
        AttestationBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> AttestationBuilder<St, S>
where
    St: attestation_state::State,
    St::Key: attestation_state::IsSet,
    St::Signature: attestation_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Attestation<S> {
        Attestation {
            attested_at: self._fields.0,
            key: self._fields.1.unwrap(),
            signature: 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>>,
    ) -> Attestation<S> {
        Attestation {
            attested_at: self._fields.0,
            key: self._fields.1.unwrap(),
            signature: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_blue_atplay_score_defs() -> 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.atplay.score.defs"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("attestation"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "Attestation signature proving a score was submitted through ATPlay SDK",
                        ),
                    ),
                    required: Some(
                        vec![
                            SmolStr::new_static("key"), SmolStr::new_static("signature")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("attestedAt"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "Timestamp when the attestation was created (optional)",
                                    ),
                                ),
                                format: Some(LexStringFormat::Datetime),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("key"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "DID key reference for verifying the signature",
                                    ),
                                ),
                                max_length: Some(512usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("signature"),
                            LexObjectProperty::Bytes(LexBytes {
                                max_length: Some(8192usize),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}