// @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, 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::{Did, AtUri, Cid, Datetime};
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 reminder scheduled to trigger at a specific time

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "net.jbsm.jb.reminder",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Reminder<S: BosStr = DefaultStr> {
    ///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")]
    pub origin_uri: Option<AtUri<S>>,
    ///DID of the user who created the reminder
    pub requester: Did<S>,
    ///The reminder message/content
    pub subject: S,
    ///Timestamp when the reminder should fire
    pub trigger_at: Datetime,
    #[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 ReminderGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Reminder<S>,
}

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

/// 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<S: BosStr> = ReminderGetRecordOutput<S>;
    type Err = RecordError;
}

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

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

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

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

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

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

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

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

impl<St: reminder_state::State, S: BosStr> ReminderBuilder<St, 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<St: reminder_state::State, S: BosStr> ReminderBuilder<St, S> {
    /// Set the `originUri` field (optional)
    pub fn origin_uri(mut self, value: impl Into<Option<AtUri<S>>>) -> 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<S>>) -> Self {
        self._fields.2 = value;
        self
    }
}

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

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

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

impl<St, S: BosStr> ReminderBuilder<St, S>
where
    St: reminder_state::State,
    St::CreatedAt: reminder_state::IsSet,
    St::Requester: reminder_state::IsSet,
    St::Subject: reminder_state::IsSet,
    St::TriggerAt: reminder_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Reminder<S> {
        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<SmolStr, Data<S>>) -> Reminder<S> {
        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()
    }
}