// @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, 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};
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

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "com.5jiji.test.videos",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Videos<S: BosStr = DefaultStr> {
    pub creator: Did<S>,
    pub id: Data<S>,
    pub title: S,
    #[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 VideosGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Videos<S>,
}

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

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

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

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

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

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

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

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

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

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

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

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

impl<St, S: BosStr> VideosBuilder<St, S>
where
    St: videos_state::State,
    St::Creator: videos_state::IsSet,
    St::Id: videos_state::IsSet,
    St::Title: videos_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Videos<S> {
        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<SmolStr, Data<S>>) -> Videos<S> {
        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()
    }
}