// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: org.devcon.event.test
//
// 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::{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};

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Test<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<Datetime>,
    ///Description of the event
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<CowStr<'a>>,
    ///End time of the event
    pub end: Datetime,
    ///Location of the event
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub location: Option<CowStr<'a>>,
    ///Start time of the event
    pub start: Datetime,
    ///Title of the event
    #[serde(borrow)]
    pub title: CowStr<'a>,
    ///URL of the event
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub url: Option<CowStr<'a>>,
}

/// 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 TestGetRecordOutput<'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: Test<'a>,
}

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct TestRecord;
impl XrpcResp for TestRecord {
    const NSID: &'static str = "org.devcon.event.test";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = TestGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

impl Collection for Test<'_> {
    const NSID: &'static str = "org.devcon.event.test";
    type Record = TestRecord;
}

impl Collection for TestRecord {
    const NSID: &'static str = "org.devcon.event.test";
    type Record = TestRecord;
}

impl<'a> LexiconSchema for Test<'a> {
    fn nsid() -> &'static str {
        "org.devcon.event.test"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_org_devcon_event_test()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

pub mod test_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 Start;
        type End;
        type Title;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Start = Unset;
        type End = Unset;
        type Title = Unset;
    }
    ///State transition - sets the `start` field to Set
    pub struct SetStart<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetStart<S> {}
    impl<S: State> State for SetStart<S> {
        type Start = Set<members::start>;
        type End = S::End;
        type Title = S::Title;
    }
    ///State transition - sets the `end` field to Set
    pub struct SetEnd<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetEnd<S> {}
    impl<S: State> State for SetEnd<S> {
        type Start = S::Start;
        type End = Set<members::end>;
        type Title = S::Title;
    }
    ///State transition - sets the `title` field to Set
    pub struct SetTitle<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetTitle<S> {}
    impl<S: State> State for SetTitle<S> {
        type Start = S::Start;
        type End = S::End;
        type Title = Set<members::title>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `start` field
        pub struct start(());
        ///Marker type for the `end` field
        pub struct end(());
        ///Marker type for the `title` field
        pub struct title(());
    }
}

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

impl<'a> Test<'a> {
    /// Create a new builder for this type
    pub fn new() -> TestBuilder<'a, test_state::Empty> {
        TestBuilder::new()
    }
}

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

impl<'a, S: test_state::State> TestBuilder<'a, S> {
    /// Set the `createdAt` field (optional)
    pub fn created_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `createdAt` field to an Option value (optional)
    pub fn maybe_created_at(mut self, value: Option<Datetime>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S: test_state::State> TestBuilder<'a, S> {
    /// Set the `description` field (optional)
    pub fn description(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `description` field to an Option value (optional)
    pub fn maybe_description(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.1 = value;
        self
    }
}

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

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

impl<'a, S> TestBuilder<'a, S>
where
    S: test_state::State,
    S::Start: test_state::IsUnset,
{
    /// Set the `start` field (required)
    pub fn start(
        mut self,
        value: impl Into<Datetime>,
    ) -> TestBuilder<'a, test_state::SetStart<S>> {
        self._fields.4 = Option::Some(value.into());
        TestBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> TestBuilder<'a, S>
where
    S: test_state::State,
    S::Title: test_state::IsUnset,
{
    /// Set the `title` field (required)
    pub fn title(
        mut self,
        value: impl Into<CowStr<'a>>,
    ) -> TestBuilder<'a, test_state::SetTitle<S>> {
        self._fields.5 = Option::Some(value.into());
        TestBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: test_state::State> TestBuilder<'a, S> {
    /// Set the `url` field (optional)
    pub fn url(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.6 = value.into();
        self
    }
    /// Set the `url` field to an Option value (optional)
    pub fn maybe_url(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.6 = value;
        self
    }
}

impl<'a, S> TestBuilder<'a, S>
where
    S: test_state::State,
    S::Start: test_state::IsSet,
    S::End: test_state::IsSet,
    S::Title: test_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Test<'a> {
        Test {
            created_at: self._fields.0,
            description: self._fields.1,
            end: self._fields.2.unwrap(),
            location: self._fields.3,
            start: self._fields.4.unwrap(),
            title: self._fields.5.unwrap(),
            url: self._fields.6,
            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>,
        >,
    ) -> Test<'a> {
        Test {
            created_at: self._fields.0,
            description: self._fields.1,
            end: self._fields.2.unwrap(),
            location: self._fields.3,
            start: self._fields.4.unwrap(),
            title: self._fields.5.unwrap(),
            url: self._fields.6,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_org_devcon_event_test() -> 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("org.devcon.event.test"),
        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 {
                        required: Some(
                            vec![
                                SmolStr::new_static("title"), SmolStr::new_static("start"),
                                SmolStr::new_static("end")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("description"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("Description of the event"),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("end"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("End time of the event"),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("location"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("Location of the event"),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("start"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("Start time of the event"),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("title"),
                                LexObjectProperty::String(LexString {
                                    description: Some(CowStr::new_static("Title of the event")),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("url"),
                                LexObjectProperty::String(LexString {
                                    description: Some(CowStr::new_static("URL of the event")),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}