// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: tech.lenooby09.didgit.signatureProof
//
// 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};
/// A cryptographic proof that a did-git object was signed by a specific DID. This provides key-rotation-independent verification by anchoring the proof to a PDS or signing event.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "tech.lenooby09.didgit.signatureProof",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct SignatureProof<S: BosStr = DefaultStr> {
    ///The hex SHA-256 object ID being proven.
    pub object_id: S,
    ///The type of object.
    pub object_type: SignatureProofObjectType<S>,
    ///The PDS endpoint that issued this proof (for service auth proofs).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pds_endpoint: Option<S>,
    ///The base64url-encoded signature or JWT token.
    pub signature: S,
    ///The signature algorithm or token type (e.g., 'atproto-service-auth', 'jws', 'raw-ed25519', 'raw-ecdsa-secp256k1').
    pub signature_type: S,
    ///Unix timestamp (seconds) when the proof was created.
    pub signed_at: i64,
    ///The DID of the signer.
    pub signer: S,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// The type of object.

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SignatureProofObjectType<S: BosStr = DefaultStr> {
    Blob,
    Tree,
    Commit,
    Tag,
    Other(S),
}

impl<S: BosStr> SignatureProofObjectType<S> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Blob => "blob",
            Self::Tree => "tree",
            Self::Commit => "commit",
            Self::Tag => "tag",
            Self::Other(s) => s.as_ref(),
        }
    }
    /// Construct from a string-like value, matching known values.
    pub fn from_value(s: S) -> Self {
        match s.as_ref() {
            "blob" => Self::Blob,
            "tree" => Self::Tree,
            "commit" => Self::Commit,
            "tag" => Self::Tag,
            _ => Self::Other(s),
        }
    }
}

impl<S: BosStr> core::fmt::Display for SignatureProofObjectType<S> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<S: BosStr> AsRef<str> for SignatureProofObjectType<S> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<S: BosStr> Serialize for SignatureProofObjectType<S> {
    fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        Ser: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de>
for SignatureProofObjectType<S> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = S::deserialize(deserializer)?;
        Ok(Self::from_value(s))
    }
}

impl<S: BosStr + Default> Default for SignatureProofObjectType<S> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl<S: BosStr> jacquard_common::IntoStatic for SignatureProofObjectType<S>
where
    S: BosStr + jacquard_common::IntoStatic,
    S::Output: BosStr,
{
    type Output = SignatureProofObjectType<S::Output>;
    fn into_static(self) -> Self::Output {
        match self {
            SignatureProofObjectType::Blob => SignatureProofObjectType::Blob,
            SignatureProofObjectType::Tree => SignatureProofObjectType::Tree,
            SignatureProofObjectType::Commit => SignatureProofObjectType::Commit,
            SignatureProofObjectType::Tag => SignatureProofObjectType::Tag,
            SignatureProofObjectType::Other(v) => {
                SignatureProofObjectType::Other(v.into_static())
            }
        }
    }
}

/// 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 SignatureProofGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: SignatureProof<S>,
}

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct SignatureProofRecord;
impl XrpcResp for SignatureProofRecord {
    const NSID: &'static str = "tech.lenooby09.didgit.signatureProof";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = SignatureProofGetRecordOutput<S>;
    type Err = RecordError;
}

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

impl<S: BosStr> Collection for SignatureProof<S> {
    const NSID: &'static str = "tech.lenooby09.didgit.signatureProof";
    type Record = SignatureProofRecord;
}

impl Collection for SignatureProofRecord {
    const NSID: &'static str = "tech.lenooby09.didgit.signatureProof";
    type Record = SignatureProofRecord;
}

impl<S: BosStr> LexiconSchema for SignatureProof<S> {
    fn nsid() -> &'static str {
        "tech.lenooby09.didgit.signatureProof"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_tech_lenooby09_didgit_signatureProof()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.object_id;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 64usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("object_id"),
                    max: 64usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.object_type;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 16usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("object_type"),
                    max: 16usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        if let Some(ref value) = self.pds_endpoint {
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 256usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("pds_endpoint"),
                    max: 256usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.signature;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 2048usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("signature"),
                    max: 2048usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.signature_type;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 64usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("signature_type"),
                    max: 64usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.signer;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 128usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("signer"),
                    max: 128usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

pub mod signature_proof_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 ObjectId;
        type ObjectType;
        type Signature;
        type SignatureType;
        type SignedAt;
        type Signer;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type ObjectId = Unset;
        type ObjectType = Unset;
        type Signature = Unset;
        type SignatureType = Unset;
        type SignedAt = Unset;
        type Signer = Unset;
    }
    ///State transition - sets the `object_id` field to Set
    pub struct SetObjectId<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetObjectId<St> {}
    impl<St: State> State for SetObjectId<St> {
        type ObjectId = Set<members::object_id>;
        type ObjectType = St::ObjectType;
        type Signature = St::Signature;
        type SignatureType = St::SignatureType;
        type SignedAt = St::SignedAt;
        type Signer = St::Signer;
    }
    ///State transition - sets the `object_type` field to Set
    pub struct SetObjectType<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetObjectType<St> {}
    impl<St: State> State for SetObjectType<St> {
        type ObjectId = St::ObjectId;
        type ObjectType = Set<members::object_type>;
        type Signature = St::Signature;
        type SignatureType = St::SignatureType;
        type SignedAt = St::SignedAt;
        type Signer = St::Signer;
    }
    ///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 ObjectId = St::ObjectId;
        type ObjectType = St::ObjectType;
        type Signature = Set<members::signature>;
        type SignatureType = St::SignatureType;
        type SignedAt = St::SignedAt;
        type Signer = St::Signer;
    }
    ///State transition - sets the `signature_type` field to Set
    pub struct SetSignatureType<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSignatureType<St> {}
    impl<St: State> State for SetSignatureType<St> {
        type ObjectId = St::ObjectId;
        type ObjectType = St::ObjectType;
        type Signature = St::Signature;
        type SignatureType = Set<members::signature_type>;
        type SignedAt = St::SignedAt;
        type Signer = St::Signer;
    }
    ///State transition - sets the `signed_at` field to Set
    pub struct SetSignedAt<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSignedAt<St> {}
    impl<St: State> State for SetSignedAt<St> {
        type ObjectId = St::ObjectId;
        type ObjectType = St::ObjectType;
        type Signature = St::Signature;
        type SignatureType = St::SignatureType;
        type SignedAt = Set<members::signed_at>;
        type Signer = St::Signer;
    }
    ///State transition - sets the `signer` field to Set
    pub struct SetSigner<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSigner<St> {}
    impl<St: State> State for SetSigner<St> {
        type ObjectId = St::ObjectId;
        type ObjectType = St::ObjectType;
        type Signature = St::Signature;
        type SignatureType = St::SignatureType;
        type SignedAt = St::SignedAt;
        type Signer = Set<members::signer>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `object_id` field
        pub struct object_id(());
        ///Marker type for the `object_type` field
        pub struct object_type(());
        ///Marker type for the `signature` field
        pub struct signature(());
        ///Marker type for the `signature_type` field
        pub struct signature_type(());
        ///Marker type for the `signed_at` field
        pub struct signed_at(());
        ///Marker type for the `signer` field
        pub struct signer(());
    }
}

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

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

impl<S: BosStr> SignatureProof<S> {
    /// Create a new builder for this type
    pub fn builder() -> SignatureProofBuilder<signature_proof_state::Empty, S> {
        SignatureProofBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> SignatureProofBuilder<St, S>
where
    St: signature_proof_state::State,
    St::ObjectId: signature_proof_state::IsUnset,
{
    /// Set the `objectId` field (required)
    pub fn object_id(
        mut self,
        value: impl Into<S>,
    ) -> SignatureProofBuilder<signature_proof_state::SetObjectId<St>, S> {
        self._fields.0 = Option::Some(value.into());
        SignatureProofBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SignatureProofBuilder<St, S>
where
    St: signature_proof_state::State,
    St::ObjectType: signature_proof_state::IsUnset,
{
    /// Set the `objectType` field (required)
    pub fn object_type(
        mut self,
        value: impl Into<SignatureProofObjectType<S>>,
    ) -> SignatureProofBuilder<signature_proof_state::SetObjectType<St>, S> {
        self._fields.1 = Option::Some(value.into());
        SignatureProofBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: signature_proof_state::State, S: BosStr> SignatureProofBuilder<St, S> {
    /// Set the `pdsEndpoint` field (optional)
    pub fn pds_endpoint(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `pdsEndpoint` field to an Option value (optional)
    pub fn maybe_pds_endpoint(mut self, value: Option<S>) -> Self {
        self._fields.2 = value;
        self
    }
}

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

impl<St, S: BosStr> SignatureProofBuilder<St, S>
where
    St: signature_proof_state::State,
    St::SignatureType: signature_proof_state::IsUnset,
{
    /// Set the `signatureType` field (required)
    pub fn signature_type(
        mut self,
        value: impl Into<S>,
    ) -> SignatureProofBuilder<signature_proof_state::SetSignatureType<St>, S> {
        self._fields.4 = Option::Some(value.into());
        SignatureProofBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SignatureProofBuilder<St, S>
where
    St: signature_proof_state::State,
    St::SignedAt: signature_proof_state::IsUnset,
{
    /// Set the `signedAt` field (required)
    pub fn signed_at(
        mut self,
        value: impl Into<i64>,
    ) -> SignatureProofBuilder<signature_proof_state::SetSignedAt<St>, S> {
        self._fields.5 = Option::Some(value.into());
        SignatureProofBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SignatureProofBuilder<St, S>
where
    St: signature_proof_state::State,
    St::Signer: signature_proof_state::IsUnset,
{
    /// Set the `signer` field (required)
    pub fn signer(
        mut self,
        value: impl Into<S>,
    ) -> SignatureProofBuilder<signature_proof_state::SetSigner<St>, S> {
        self._fields.6 = Option::Some(value.into());
        SignatureProofBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SignatureProofBuilder<St, S>
where
    St: signature_proof_state::State,
    St::ObjectId: signature_proof_state::IsSet,
    St::ObjectType: signature_proof_state::IsSet,
    St::Signature: signature_proof_state::IsSet,
    St::SignatureType: signature_proof_state::IsSet,
    St::SignedAt: signature_proof_state::IsSet,
    St::Signer: signature_proof_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> SignatureProof<S> {
        SignatureProof {
            object_id: self._fields.0.unwrap(),
            object_type: self._fields.1.unwrap(),
            pds_endpoint: self._fields.2,
            signature: self._fields.3.unwrap(),
            signature_type: self._fields.4.unwrap(),
            signed_at: self._fields.5.unwrap(),
            signer: self._fields.6.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>>,
    ) -> SignatureProof<S> {
        SignatureProof {
            object_id: self._fields.0.unwrap(),
            object_type: self._fields.1.unwrap(),
            pds_endpoint: self._fields.2,
            signature: self._fields.3.unwrap(),
            signature_type: self._fields.4.unwrap(),
            signed_at: self._fields.5.unwrap(),
            signer: self._fields.6.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_tech_lenooby09_didgit_signatureProof() -> 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("tech.lenooby09.didgit.signatureProof"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "A cryptographic proof that a did-git object was signed by a specific DID. This provides key-rotation-independent verification by anchoring the proof to a PDS or signing event.",
                        ),
                    ),
                    key: Some(CowStr::new_static("any")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("objectId"),
                                SmolStr::new_static("objectType"),
                                SmolStr::new_static("signatureType"),
                                SmolStr::new_static("signature"),
                                SmolStr::new_static("signedAt"),
                                SmolStr::new_static("signer")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("objectId"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The hex SHA-256 object ID being proven.",
                                        ),
                                    ),
                                    max_length: Some(64usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("objectType"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("The type of object."),
                                    ),
                                    max_length: Some(16usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("pdsEndpoint"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The PDS endpoint that issued this proof (for service auth proofs).",
                                        ),
                                    ),
                                    max_length: Some(256usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("signature"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The base64url-encoded signature or JWT token.",
                                        ),
                                    ),
                                    max_length: Some(2048usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("signatureType"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The signature algorithm or token type (e.g., 'atproto-service-auth', 'jws', 'raw-ed25519', 'raw-ecdsa-secp256k1').",
                                        ),
                                    ),
                                    max_length: Some(64usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("signedAt"),
                                LexObjectProperty::Integer(LexInteger {
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("signer"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("The DID of the signer."),
                                    ),
                                    max_length: Some(128usize),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}