// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: net.jbsm.jb.reminder
//
// 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::{Did, AtUri, Cid, Datetime};
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};
/// A reminder scheduled to trigger at a specific time

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Reminder<'a> {
    ///Timestamp when the reminder was created
    pub created_at: Datetime,
    ///Whether the reminder has been triggered  Defaults to `false`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "_default_reminder_occurred")]
    pub occurred: Option<bool>,
    ///AT-URI of the original post/message that triggered this reminder
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub origin_uri: Option<AtUri<'a>>,
    ///DID of the user who created the reminder
    #[serde(borrow)]
    pub requester: Did<'a>,
    ///The reminder message/content
    #[serde(borrow)]
    pub subject: CowStr<'a>,
    ///Timestamp when the reminder should fire
    pub trigger_at: Datetime,
}

/// 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 ReminderGetRecordOutput<'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: Reminder<'a>,
}

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct ReminderRecord;
impl XrpcResp for ReminderRecord {
    const NSID: &'static str = "net.jbsm.jb.reminder";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = ReminderGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

impl Collection for Reminder<'_> {
    const NSID: &'static str = "net.jbsm.jb.reminder";
    type Record = ReminderRecord;
}

impl Collection for ReminderRecord {
    const NSID: &'static str = "net.jbsm.jb.reminder";
    type Record = ReminderRecord;
}

impl<'a> LexiconSchema for Reminder<'a> {
    fn nsid() -> &'static str {
        "net.jbsm.jb.reminder"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_net_jbsm_jb_reminder()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.subject;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 1000usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("subject"),
                    max: 1000usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

fn _default_reminder_occurred() -> Option<bool> {
    Some(false)
}

pub mod reminder_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 CreatedAt;
        type TriggerAt;
        type Subject;
        type Requester;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type CreatedAt = Unset;
        type TriggerAt = Unset;
        type Subject = Unset;
        type Requester = Unset;
    }
    ///State transition - sets the `created_at` field to Set
    pub struct SetCreatedAt<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetCreatedAt<S> {}
    impl<S: State> State for SetCreatedAt<S> {
        type CreatedAt = Set<members::created_at>;
        type TriggerAt = S::TriggerAt;
        type Subject = S::Subject;
        type Requester = S::Requester;
    }
    ///State transition - sets the `trigger_at` field to Set
    pub struct SetTriggerAt<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetTriggerAt<S> {}
    impl<S: State> State for SetTriggerAt<S> {
        type CreatedAt = S::CreatedAt;
        type TriggerAt = Set<members::trigger_at>;
        type Subject = S::Subject;
        type Requester = S::Requester;
    }
    ///State transition - sets the `subject` field to Set
    pub struct SetSubject<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetSubject<S> {}
    impl<S: State> State for SetSubject<S> {
        type CreatedAt = S::CreatedAt;
        type TriggerAt = S::TriggerAt;
        type Subject = Set<members::subject>;
        type Requester = S::Requester;
    }
    ///State transition - sets the `requester` field to Set
    pub struct SetRequester<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetRequester<S> {}
    impl<S: State> State for SetRequester<S> {
        type CreatedAt = S::CreatedAt;
        type TriggerAt = S::TriggerAt;
        type Subject = S::Subject;
        type Requester = Set<members::requester>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `created_at` field
        pub struct created_at(());
        ///Marker type for the `trigger_at` field
        pub struct trigger_at(());
        ///Marker type for the `subject` field
        pub struct subject(());
        ///Marker type for the `requester` field
        pub struct requester(());
    }
}

/// Builder for constructing an instance of this type
pub struct ReminderBuilder<'a, S: reminder_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<Datetime>,
        Option<bool>,
        Option<AtUri<'a>>,
        Option<Did<'a>>,
        Option<CowStr<'a>>,
        Option<Datetime>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Reminder<'a> {
    /// Create a new builder for this type
    pub fn new() -> ReminderBuilder<'a, reminder_state::Empty> {
        ReminderBuilder::new()
    }
}

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

impl<'a, S> ReminderBuilder<'a, S>
where
    S: reminder_state::State,
    S::CreatedAt: reminder_state::IsUnset,
{
    /// Set the `createdAt` field (required)
    pub fn created_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> ReminderBuilder<'a, reminder_state::SetCreatedAt<S>> {
        self._fields.0 = Option::Some(value.into());
        ReminderBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: reminder_state::State> ReminderBuilder<'a, S> {
    /// Set the `occurred` field (optional)
    pub fn occurred(mut self, value: impl Into<Option<bool>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `occurred` field to an Option value (optional)
    pub fn maybe_occurred(mut self, value: Option<bool>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S: reminder_state::State> ReminderBuilder<'a, S> {
    /// Set the `originUri` field (optional)
    pub fn origin_uri(mut self, value: impl Into<Option<AtUri<'a>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `originUri` field to an Option value (optional)
    pub fn maybe_origin_uri(mut self, value: Option<AtUri<'a>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<'a, S> ReminderBuilder<'a, S>
where
    S: reminder_state::State,
    S::Requester: reminder_state::IsUnset,
{
    /// Set the `requester` field (required)
    pub fn requester(
        mut self,
        value: impl Into<Did<'a>>,
    ) -> ReminderBuilder<'a, reminder_state::SetRequester<S>> {
        self._fields.3 = Option::Some(value.into());
        ReminderBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ReminderBuilder<'a, S>
where
    S: reminder_state::State,
    S::Subject: reminder_state::IsUnset,
{
    /// Set the `subject` field (required)
    pub fn subject(
        mut self,
        value: impl Into<CowStr<'a>>,
    ) -> ReminderBuilder<'a, reminder_state::SetSubject<S>> {
        self._fields.4 = Option::Some(value.into());
        ReminderBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ReminderBuilder<'a, S>
where
    S: reminder_state::State,
    S::TriggerAt: reminder_state::IsUnset,
{
    /// Set the `triggerAt` field (required)
    pub fn trigger_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> ReminderBuilder<'a, reminder_state::SetTriggerAt<S>> {
        self._fields.5 = Option::Some(value.into());
        ReminderBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ReminderBuilder<'a, S>
where
    S: reminder_state::State,
    S::CreatedAt: reminder_state::IsSet,
    S::TriggerAt: reminder_state::IsSet,
    S::Subject: reminder_state::IsSet,
    S::Requester: reminder_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Reminder<'a> {
        Reminder {
            created_at: self._fields.0.unwrap(),
            occurred: self._fields.1.or_else(|| Some(false)),
            origin_uri: self._fields.2,
            requester: self._fields.3.unwrap(),
            subject: self._fields.4.unwrap(),
            trigger_at: self._fields.5.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>,
        >,
    ) -> Reminder<'a> {
        Reminder {
            created_at: self._fields.0.unwrap(),
            occurred: self._fields.1.or_else(|| Some(false)),
            origin_uri: self._fields.2,
            requester: self._fields.3.unwrap(),
            subject: self._fields.4.unwrap(),
            trigger_at: self._fields.5.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_net_jbsm_jb_reminder() -> 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("net.jbsm.jb.reminder"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "A reminder scheduled to trigger at a specific time",
                        ),
                    ),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("createdAt"),
                                SmolStr::new_static("triggerAt"),
                                SmolStr::new_static("subject"),
                                SmolStr::new_static("requester")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Timestamp when the reminder was created",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("occurred"),
                                LexObjectProperty::Boolean(LexBoolean {
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("originUri"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "AT-URI of the original post/message that triggered this reminder",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::AtUri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("requester"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "DID of the user who created the reminder",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Did),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("subject"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("The reminder message/content"),
                                    ),
                                    max_length: Some(1000usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("triggerAt"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Timestamp when the reminder should fire",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}