// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: social.pmsky.vote
//
// 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;
use jacquard_common::deps::bytes::Bytes;

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{Did, AtUri, Cid, Datetime, UriValue};
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};

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Vote<'a> {
    ///The persistent, anonymous identifier for the user casting the vote.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub aid: Option<CowStr<'a>>,
    ///Optionally, CID specifying the specific version of 'uri' resource this vote applies to.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
    ///Timestamp when this vote was created.
    pub cts: Datetime,
    ///An optional array of predefined reasons justifying the vote.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub reasons: Option<Vec<CowStr<'a>>>,
    ///Signature of dag-cbor encoded vote.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, with = "jacquard_common::opt_serde_bytes_helper")]
    pub sig: Option<Bytes>,
    ///the account creating the vote, not necessarily the same as the user who voted
    #[serde(borrow)]
    pub src: Did<'a>,
    ///AT URI of the record, repository (account), or other resource that this vote applies to.
    #[serde(borrow)]
    pub uri: UriValue<'a>,
    ///The value of the vote. The exact meaning depends on what is being voted on, but generally '+1' means 'approval', -1 means 'disapproval', and 0 indicates 'neutrality'.
    pub val: i64,
}

/// 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 VoteGetRecordOutput<'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: Vote<'a>,
}

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct VoteRecord;
impl XrpcResp for VoteRecord {
    const NSID: &'static str = "social.pmsky.vote";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = VoteGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

impl Collection for Vote<'_> {
    const NSID: &'static str = "social.pmsky.vote";
    type Record = VoteRecord;
}

impl Collection for VoteRecord {
    const NSID: &'static str = "social.pmsky.vote";
    type Record = VoteRecord;
}

impl<'a> LexiconSchema for Vote<'a> {
    fn nsid() -> &'static str {
        "social.pmsky.vote"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_social_pmsky_vote()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

pub mod vote_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 Uri;
        type Src;
        type Cts;
        type Val;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Uri = Unset;
        type Src = Unset;
        type Cts = Unset;
        type Val = Unset;
    }
    ///State transition - sets the `uri` field to Set
    pub struct SetUri<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetUri<S> {}
    impl<S: State> State for SetUri<S> {
        type Uri = Set<members::uri>;
        type Src = S::Src;
        type Cts = S::Cts;
        type Val = S::Val;
    }
    ///State transition - sets the `src` field to Set
    pub struct SetSrc<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetSrc<S> {}
    impl<S: State> State for SetSrc<S> {
        type Uri = S::Uri;
        type Src = Set<members::src>;
        type Cts = S::Cts;
        type Val = S::Val;
    }
    ///State transition - sets the `cts` field to Set
    pub struct SetCts<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetCts<S> {}
    impl<S: State> State for SetCts<S> {
        type Uri = S::Uri;
        type Src = S::Src;
        type Cts = Set<members::cts>;
        type Val = S::Val;
    }
    ///State transition - sets the `val` field to Set
    pub struct SetVal<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetVal<S> {}
    impl<S: State> State for SetVal<S> {
        type Uri = S::Uri;
        type Src = S::Src;
        type Cts = S::Cts;
        type Val = Set<members::val>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `uri` field
        pub struct uri(());
        ///Marker type for the `src` field
        pub struct src(());
        ///Marker type for the `cts` field
        pub struct cts(());
        ///Marker type for the `val` field
        pub struct val(());
    }
}

/// Builder for constructing an instance of this type
pub struct VoteBuilder<'a, S: vote_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<CowStr<'a>>,
        Option<Cid<'a>>,
        Option<Datetime>,
        Option<Vec<CowStr<'a>>>,
        Option<Bytes>,
        Option<Did<'a>>,
        Option<UriValue<'a>>,
        Option<i64>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Vote<'a> {
    /// Create a new builder for this type
    pub fn new() -> VoteBuilder<'a, vote_state::Empty> {
        VoteBuilder::new()
    }
}

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

impl<'a, S: vote_state::State> VoteBuilder<'a, S> {
    /// Set the `aid` field (optional)
    pub fn aid(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `aid` field to an Option value (optional)
    pub fn maybe_aid(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S: vote_state::State> VoteBuilder<'a, S> {
    /// Set the `cid` field (optional)
    pub fn cid(mut self, value: impl Into<Option<Cid<'a>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `cid` field to an Option value (optional)
    pub fn maybe_cid(mut self, value: Option<Cid<'a>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S> VoteBuilder<'a, S>
where
    S: vote_state::State,
    S::Cts: vote_state::IsUnset,
{
    /// Set the `cts` field (required)
    pub fn cts(
        mut self,
        value: impl Into<Datetime>,
    ) -> VoteBuilder<'a, vote_state::SetCts<S>> {
        self._fields.2 = Option::Some(value.into());
        VoteBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: vote_state::State> VoteBuilder<'a, S> {
    /// Set the `reasons` field (optional)
    pub fn reasons(mut self, value: impl Into<Option<Vec<CowStr<'a>>>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `reasons` field to an Option value (optional)
    pub fn maybe_reasons(mut self, value: Option<Vec<CowStr<'a>>>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<'a, S: vote_state::State> VoteBuilder<'a, S> {
    /// Set the `sig` field (optional)
    pub fn sig(mut self, value: impl Into<Option<Bytes>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `sig` field to an Option value (optional)
    pub fn maybe_sig(mut self, value: Option<Bytes>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<'a, S> VoteBuilder<'a, S>
where
    S: vote_state::State,
    S::Src: vote_state::IsUnset,
{
    /// Set the `src` field (required)
    pub fn src(
        mut self,
        value: impl Into<Did<'a>>,
    ) -> VoteBuilder<'a, vote_state::SetSrc<S>> {
        self._fields.5 = Option::Some(value.into());
        VoteBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> VoteBuilder<'a, S>
where
    S: vote_state::State,
    S::Uri: vote_state::IsUnset,
{
    /// Set the `uri` field (required)
    pub fn uri(
        mut self,
        value: impl Into<UriValue<'a>>,
    ) -> VoteBuilder<'a, vote_state::SetUri<S>> {
        self._fields.6 = Option::Some(value.into());
        VoteBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> VoteBuilder<'a, S>
where
    S: vote_state::State,
    S::Val: vote_state::IsUnset,
{
    /// Set the `val` field (required)
    pub fn val(
        mut self,
        value: impl Into<i64>,
    ) -> VoteBuilder<'a, vote_state::SetVal<S>> {
        self._fields.7 = Option::Some(value.into());
        VoteBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> VoteBuilder<'a, S>
where
    S: vote_state::State,
    S::Uri: vote_state::IsSet,
    S::Src: vote_state::IsSet,
    S::Cts: vote_state::IsSet,
    S::Val: vote_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Vote<'a> {
        Vote {
            aid: self._fields.0,
            cid: self._fields.1,
            cts: self._fields.2.unwrap(),
            reasons: self._fields.3,
            sig: self._fields.4,
            src: self._fields.5.unwrap(),
            uri: self._fields.6.unwrap(),
            val: self._fields.7.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>,
        >,
    ) -> Vote<'a> {
        Vote {
            aid: self._fields.0,
            cid: self._fields.1,
            cts: self._fields.2.unwrap(),
            reasons: self._fields.3,
            sig: self._fields.4,
            src: self._fields.5.unwrap(),
            uri: self._fields.6.unwrap(),
            val: self._fields.7.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_social_pmsky_vote() -> 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("social.pmsky.vote"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        description: Some(
                            CowStr::new_static(
                                "A vote record, representing a user's approval or disapproval of the referenced resource. The resource my be a proposal, a post, a web page, or anything that can be agreed or disagreed with.",
                            ),
                        ),
                        required: Some(
                            vec![
                                SmolStr::new_static("src"), SmolStr::new_static("uri"),
                                SmolStr::new_static("val"), SmolStr::new_static("cts")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("aid"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The persistent, anonymous identifier for the user casting the vote.",
                                        ),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("cid"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Optionally, CID specifying the specific version of 'uri' resource this vote applies to.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Cid),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("cts"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("Timestamp when this vote was created."),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("reasons"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static(
                                            "An optional array of predefined reasons justifying the vote.",
                                        ),
                                    ),
                                    items: LexArrayItem::String(LexString {
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("sig"),
                                LexObjectProperty::Bytes(LexBytes { ..Default::default() }),
                            );
                            map.insert(
                                SmolStr::new_static("src"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "the account creating the vote, not necessarily the same as the user who voted",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Did),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("uri"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "AT URI of the record, repository (account), or other resource that this vote applies to.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Uri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("val"),
                                LexObjectProperty::Integer(LexInteger {
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}