// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: com.5jiji.test.videos
//
// 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};
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 video

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Videos<'a> {
    #[serde(borrow)]
    pub creator: Did<'a>,
    #[serde(borrow)]
    pub id: Data<'a>,
    #[serde(borrow)]
    pub title: 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 VideosGetRecordOutput<'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: Videos<'a>,
}

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct VideosRecord;
impl XrpcResp for VideosRecord {
    const NSID: &'static str = "com.5jiji.test.videos";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = VideosGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

impl Collection for Videos<'_> {
    const NSID: &'static str = "com.5jiji.test.videos";
    type Record = VideosRecord;
}

impl Collection for VideosRecord {
    const NSID: &'static str = "com.5jiji.test.videos";
    type Record = VideosRecord;
}

impl<'a> LexiconSchema for Videos<'a> {
    fn nsid() -> &'static str {
        "com.5jiji.test.videos"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_com_5jiji_test_videos()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.title;
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 150usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("title"),
                        max: 150usize,
                        actual: count,
                    });
                }
            }
        }
        Ok(())
    }
}

pub mod videos_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 Id;
        type Title;
        type Creator;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Id = Unset;
        type Title = Unset;
        type Creator = Unset;
    }
    ///State transition - sets the `id` field to Set
    pub struct SetId<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetId<S> {}
    impl<S: State> State for SetId<S> {
        type Id = Set<members::id>;
        type Title = S::Title;
        type Creator = S::Creator;
    }
    ///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 Id = S::Id;
        type Title = Set<members::title>;
        type Creator = S::Creator;
    }
    ///State transition - sets the `creator` field to Set
    pub struct SetCreator<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetCreator<S> {}
    impl<S: State> State for SetCreator<S> {
        type Id = S::Id;
        type Title = S::Title;
        type Creator = Set<members::creator>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `id` field
        pub struct id(());
        ///Marker type for the `title` field
        pub struct title(());
        ///Marker type for the `creator` field
        pub struct creator(());
    }
}

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

impl<'a> Videos<'a> {
    /// Create a new builder for this type
    pub fn new() -> VideosBuilder<'a, videos_state::Empty> {
        VideosBuilder::new()
    }
}

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

impl<'a, S> VideosBuilder<'a, S>
where
    S: videos_state::State,
    S::Creator: videos_state::IsUnset,
{
    /// Set the `creator` field (required)
    pub fn creator(
        mut self,
        value: impl Into<Did<'a>>,
    ) -> VideosBuilder<'a, videos_state::SetCreator<S>> {
        self._fields.0 = Option::Some(value.into());
        VideosBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> VideosBuilder<'a, S>
where
    S: videos_state::State,
    S::Id: videos_state::IsUnset,
{
    /// Set the `id` field (required)
    pub fn id(
        mut self,
        value: impl Into<Data<'a>>,
    ) -> VideosBuilder<'a, videos_state::SetId<S>> {
        self._fields.1 = Option::Some(value.into());
        VideosBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

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

impl<'a, S> VideosBuilder<'a, S>
where
    S: videos_state::State,
    S::Id: videos_state::IsSet,
    S::Title: videos_state::IsSet,
    S::Creator: videos_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Videos<'a> {
        Videos {
            creator: self._fields.0.unwrap(),
            id: self._fields.1.unwrap(),
            title: self._fields.2.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, Data<'a>>,
    ) -> Videos<'a> {
        Videos {
            creator: self._fields.0.unwrap(),
            id: self._fields.1.unwrap(),
            title: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_com_5jiji_test_videos() -> 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("com.5jiji.test.videos"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(CowStr::new_static("A video")),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("id"), SmolStr::new_static("title"),
                                SmolStr::new_static("creator")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("creator"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Did),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("id"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("com.5jiji.test.vidId"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("title"),
                                LexObjectProperty::String(LexString {
                                    max_graphemes: Some(150usize),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}