// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: social.pmsky.proposal
//
// 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::collection::{Collection, RecordError};
use jacquard_common::types::string::{Did, AtUri, Cid, Datetime, UriValue};
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};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "social.pmsky.proposal",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Proposal<S: BosStr = DefaultStr> {
    ///The persistent, anonymous identifier for the user creating the proposal.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aid: Option<S>,
    ///Optionally, CID specifying the specific version of 'uri' resource this proposal applies to.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    ///Timestamp when this proposal was created.
    pub cts: Datetime,
    ///For 'label' proposals where 'val' is 'needs-context', the full text of any proposed annotation (e.g. community note) to be shown below the post.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<S>,
    ///An optional array of predefined reasons justifying the moderation action.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasons: Option<Vec<S>>,
    ///Signature of dag-cbor encoded proposal.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, with = "jacquard_common::opt_serde_bytes_helper")]
    pub sig: Option<Bytes>,
    ///DID of the actor who created this proposal.
    pub src: Did<S>,
    ///The type of moderation action being proposed. Currently expected values are 'allowed_user' or 'label'
    pub typ: S,
    ///AT URI of the record, repository (account), or other resource that this proposal applies to.
    pub uri: UriValue<S>,
    ///For 'label' proposals, the short string name of the value of the proposed label.
    pub val: S,
    ///The AT Protocol version of the proposal object.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ver: Option<i64>,
    #[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 ProposalGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Proposal<S>,
}

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

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

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

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

impl<S: BosStr> Collection for Proposal<S> {
    const NSID: &'static str = "social.pmsky.proposal";
    type Record = ProposalRecord;
}

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

impl<S: BosStr> LexiconSchema for Proposal<S> {
    fn nsid() -> &'static str {
        "social.pmsky.proposal"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_social_pmsky_proposal()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.val;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 128usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("val"),
                    max: 128usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type.
pub struct ProposalBuilder<St: proposal_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (
        Option<S>,
        Option<Cid<S>>,
        Option<Datetime>,
        Option<S>,
        Option<Vec<S>>,
        Option<Bytes>,
        Option<Did<S>>,
        Option<S>,
        Option<UriValue<S>>,
        Option<S>,
        Option<i64>,
    ),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> Proposal<S> {
    /// Create a new builder for this type
    pub fn builder() -> ProposalBuilder<proposal_state::Empty, S> {
        ProposalBuilder::builder()
    }
}

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

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

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

impl<St: proposal_state::State, S: BosStr> ProposalBuilder<St, S> {
    /// Set the `cid` field (optional)
    pub fn cid(mut self, value: impl Into<Option<Cid<S>>>) -> 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<S>>) -> Self {
        self._fields.1 = value;
        self
    }
}

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

impl<St: proposal_state::State, S: BosStr> ProposalBuilder<St, S> {
    /// Set the `note` field (optional)
    pub fn note(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `note` field to an Option value (optional)
    pub fn maybe_note(mut self, value: Option<S>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<St: proposal_state::State, S: BosStr> ProposalBuilder<St, S> {
    /// Set the `reasons` field (optional)
    pub fn reasons(mut self, value: impl Into<Option<Vec<S>>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `reasons` field to an Option value (optional)
    pub fn maybe_reasons(mut self, value: Option<Vec<S>>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<St: proposal_state::State, S: BosStr> ProposalBuilder<St, S> {
    /// Set the `sig` field (optional)
    pub fn sig(mut self, value: impl Into<Option<Bytes>>) -> Self {
        self._fields.5 = value.into();
        self
    }
    /// Set the `sig` field to an Option value (optional)
    pub fn maybe_sig(mut self, value: Option<Bytes>) -> Self {
        self._fields.5 = value;
        self
    }
}

impl<St, S: BosStr> ProposalBuilder<St, S>
where
    St: proposal_state::State,
    St::Src: proposal_state::IsUnset,
{
    /// Set the `src` field (required)
    pub fn src(
        mut self,
        value: impl Into<Did<S>>,
    ) -> ProposalBuilder<proposal_state::SetSrc<St>, S> {
        self._fields.6 = Option::Some(value.into());
        ProposalBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ProposalBuilder<St, S>
where
    St: proposal_state::State,
    St::Typ: proposal_state::IsUnset,
{
    /// Set the `typ` field (required)
    pub fn typ(
        mut self,
        value: impl Into<S>,
    ) -> ProposalBuilder<proposal_state::SetTyp<St>, S> {
        self._fields.7 = Option::Some(value.into());
        ProposalBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ProposalBuilder<St, S>
where
    St: proposal_state::State,
    St::Uri: proposal_state::IsUnset,
{
    /// Set the `uri` field (required)
    pub fn uri(
        mut self,
        value: impl Into<UriValue<S>>,
    ) -> ProposalBuilder<proposal_state::SetUri<St>, S> {
        self._fields.8 = Option::Some(value.into());
        ProposalBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ProposalBuilder<St, S>
where
    St: proposal_state::State,
    St::Val: proposal_state::IsUnset,
{
    /// Set the `val` field (required)
    pub fn val(
        mut self,
        value: impl Into<S>,
    ) -> ProposalBuilder<proposal_state::SetVal<St>, S> {
        self._fields.9 = Option::Some(value.into());
        ProposalBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: proposal_state::State, S: BosStr> ProposalBuilder<St, S> {
    /// Set the `ver` field (optional)
    pub fn ver(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.10 = value.into();
        self
    }
    /// Set the `ver` field to an Option value (optional)
    pub fn maybe_ver(mut self, value: Option<i64>) -> Self {
        self._fields.10 = value;
        self
    }
}

impl<St, S: BosStr> ProposalBuilder<St, S>
where
    St: proposal_state::State,
    St::Cts: proposal_state::IsSet,
    St::Src: proposal_state::IsSet,
    St::Typ: proposal_state::IsSet,
    St::Uri: proposal_state::IsSet,
    St::Val: proposal_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Proposal<S> {
        Proposal {
            aid: self._fields.0,
            cid: self._fields.1,
            cts: self._fields.2.unwrap(),
            note: self._fields.3,
            reasons: self._fields.4,
            sig: self._fields.5,
            src: self._fields.6.unwrap(),
            typ: self._fields.7.unwrap(),
            uri: self._fields.8.unwrap(),
            val: self._fields.9.unwrap(),
            ver: self._fields.10,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Proposal<S> {
        Proposal {
            aid: self._fields.0,
            cid: self._fields.1,
            cts: self._fields.2.unwrap(),
            note: self._fields.3,
            reasons: self._fields.4,
            sig: self._fields.5,
            src: self._fields.6.unwrap(),
            typ: self._fields.7.unwrap(),
            uri: self._fields.8.unwrap(),
            val: self._fields.9.unwrap(),
            ver: self._fields.10,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_social_pmsky_proposal() -> 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.proposal"),
        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 proposed moderation action (e.g. adding a label or annotation to a post). Refers to some other resource via URI (e.g. an atproto post). Superset of 'com.atproto.proposal.defs#label.",
                            ),
                        ),
                        required: Some(
                            vec![
                                SmolStr::new_static("typ"), 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 creating the proposal.",
                                        ),
                                    ),
                                    ..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 proposal 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 proposal was created.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("note"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "For 'label' proposals where 'val' is 'needs-context', the full text of any proposed annotation (e.g. community note) to be shown below the post.",
                                        ),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("reasons"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static(
                                            "An optional array of predefined reasons justifying the moderation action.",
                                        ),
                                    ),
                                    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(
                                            "DID of the actor who created this proposal.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Did),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("typ"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "The type of moderation action being proposed. Currently expected values are 'allowed_user' or 'label'",
                                        ),
                                    ),
                                    ..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 proposal applies to.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Uri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("val"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "For 'label' proposals, the short string name of the value of the proposed label.",
                                        ),
                                    ),
                                    max_length: Some(128usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("ver"),
                                LexObjectProperty::Integer(LexInteger {
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}