// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.stream.media.track
//
// 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::{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};
use crate::app_bsky::actor::ProfileViewBasic;
use crate::com_atproto::repo::strong_ref::StrongRef;
use crate::place_stream::media::MuxlTrack;
use crate::place_stream::segment::Audio;
use crate::place_stream::segment::Video;
use crate::place_stream::media::track;
/// Metadata common to all media types. Contains subobjects for other media types.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct CommonMetadata<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub audio: Option<Audio<S>>,
    ///duration of this track in milliseconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_ms: Option<i64>,
    ///IETF BCP 47 language tag corresponding to the content of this track
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video: Option<Video<S>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// A track for a video stream, either part of the source or a custom additional track. One of: video, audio, subtitles.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "place.stream.media.track",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Track<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<track::CommonMetadata<S>>,
    ///If this is a derived track like a transcode or a transcript, what was the parent track?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_track: Option<StrongRef<S>>,
    pub track: MuxlTrack<S>,
    ///If this is a derived track like a transcode or a transcript, what was the source place.stream.video?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video: Option<AtUri<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 TrackGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Track<S>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct TrackView<S: BosStr = DefaultStr> {
    pub author: ProfileViewBasic<S>,
    pub cid: Cid<S>,
    pub record: Data<S>,
    pub uri: AtUri<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

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

impl<S: BosStr> LexiconSchema for CommonMetadata<S> {
    fn nsid() -> &'static str {
        "place.stream.media.track"
    }
    fn def_name() -> &'static str {
        "commonMetadata"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_media_track()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

#[derive(Debug, Serialize, Deserialize)]
pub struct TrackRecord;
impl XrpcResp for TrackRecord {
    const NSID: &'static str = "place.stream.media.track";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = TrackGetRecordOutput<S>;
    type Err = RecordError;
}

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

impl<S: BosStr> Collection for Track<S> {
    const NSID: &'static str = "place.stream.media.track";
    type Record = TrackRecord;
}

impl Collection for TrackRecord {
    const NSID: &'static str = "place.stream.media.track";
    type Record = TrackRecord;
}

impl<S: BosStr> LexiconSchema for Track<S> {
    fn nsid() -> &'static str {
        "place.stream.media.track"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_media_track()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

impl<S: BosStr> LexiconSchema for TrackView<S> {
    fn nsid() -> &'static str {
        "place.stream.media.track"
    }
    fn def_name() -> &'static str {
        "trackView"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_media_track()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

fn lexicon_doc_place_stream_media_track() -> 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("place.stream.media.track"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("commonMetadata"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "Metadata common to all media types. Contains subobjects for other media types.",
                        ),
                    ),
                    required: Some(vec![]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("audio"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static("place.stream.segment#audio"),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("durationMs"),
                            LexObjectProperty::Integer(LexInteger {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("language"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "IETF BCP 47 language tag corresponding to the content of this track",
                                    ),
                                ),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("video"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static("place.stream.segment#video"),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "A track for a video stream, either part of the source or a custom additional track. One of: video, audio, subtitles.",
                        ),
                    ),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(vec![SmolStr::new_static("track")]),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("metadata"),
                                LexObjectProperty::Union(LexRefUnion {
                                    refs: vec![
                                        CowStr::new_static("place.stream.media.track#commonMetadata")
                                    ],
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("parentTrack"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("com.atproto.repo.strongRef"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("track"),
                                LexObjectProperty::Union(LexRefUnion {
                                    refs: vec![
                                        CowStr::new_static("place.stream.media.defs#muxlTrack")
                                    ],
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("video"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "If this is a derived track like a transcode or a transcript, what was the source place.stream.video?",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::AtUri),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("trackView"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![
                            SmolStr::new_static("uri"), SmolStr::new_static("cid"),
                            SmolStr::new_static("author"), SmolStr::new_static("record")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("author"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static(
                                    "app.bsky.actor.defs#profileViewBasic",
                                ),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("cid"),
                            LexObjectProperty::String(LexString {
                                format: Some(LexStringFormat::Cid),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("record"),
                            LexObjectProperty::Unknown(LexUnknown {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("uri"),
                            LexObjectProperty::String(LexString {
                                format: Some(LexStringFormat::AtUri),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

pub mod track_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 Track;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Track = Unset;
    }
    ///State transition - sets the `track` field to Set
    pub struct SetTrack<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetTrack<St> {}
    impl<St: State> State for SetTrack<St> {
        type Track = Set<members::track>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `track` field
        pub struct track(());
    }
}

/// Builder for constructing an instance of this type.
pub struct TrackBuilder<St: track_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (
        Option<track::CommonMetadata<S>>,
        Option<StrongRef<S>>,
        Option<MuxlTrack<S>>,
        Option<AtUri<S>>,
    ),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> Track<S> {
    /// Create a new builder for this type
    pub fn builder() -> TrackBuilder<track_state::Empty, S> {
        TrackBuilder::builder()
    }
}

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

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

impl<St: track_state::State, S: BosStr> TrackBuilder<St, S> {
    /// Set the `metadata` field (optional)
    pub fn metadata(
        mut self,
        value: impl Into<Option<track::CommonMetadata<S>>>,
    ) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `metadata` field to an Option value (optional)
    pub fn maybe_metadata(mut self, value: Option<track::CommonMetadata<S>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<St: track_state::State, S: BosStr> TrackBuilder<St, S> {
    /// Set the `parentTrack` field (optional)
    pub fn parent_track(mut self, value: impl Into<Option<StrongRef<S>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `parentTrack` field to an Option value (optional)
    pub fn maybe_parent_track(mut self, value: Option<StrongRef<S>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> TrackBuilder<St, S>
where
    St: track_state::State,
    St::Track: track_state::IsUnset,
{
    /// Set the `track` field (required)
    pub fn track(
        mut self,
        value: impl Into<MuxlTrack<S>>,
    ) -> TrackBuilder<track_state::SetTrack<St>, S> {
        self._fields.2 = Option::Some(value.into());
        TrackBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: track_state::State, S: BosStr> TrackBuilder<St, S> {
    /// Set the `video` field (optional)
    pub fn video(mut self, value: impl Into<Option<AtUri<S>>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `video` field to an Option value (optional)
    pub fn maybe_video(mut self, value: Option<AtUri<S>>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<St, S: BosStr> TrackBuilder<St, S>
where
    St: track_state::State,
    St::Track: track_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Track<S> {
        Track {
            metadata: self._fields.0,
            parent_track: self._fields.1,
            track: self._fields.2.unwrap(),
            video: self._fields.3,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Track<S> {
        Track {
            metadata: self._fields.0,
            parent_track: self._fields.1,
            track: self._fields.2.unwrap(),
            video: self._fields.3,
            extra_data: Some(extra_data),
        }
    }
}

pub mod track_view_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 Author;
        type Cid;
        type Record;
        type Uri;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Author = Unset;
        type Cid = Unset;
        type Record = Unset;
        type Uri = Unset;
    }
    ///State transition - sets the `author` field to Set
    pub struct SetAuthor<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetAuthor<St> {}
    impl<St: State> State for SetAuthor<St> {
        type Author = Set<members::author>;
        type Cid = St::Cid;
        type Record = St::Record;
        type Uri = St::Uri;
    }
    ///State transition - sets the `cid` field to Set
    pub struct SetCid<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetCid<St> {}
    impl<St: State> State for SetCid<St> {
        type Author = St::Author;
        type Cid = Set<members::cid>;
        type Record = St::Record;
        type Uri = St::Uri;
    }
    ///State transition - sets the `record` field to Set
    pub struct SetRecord<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetRecord<St> {}
    impl<St: State> State for SetRecord<St> {
        type Author = St::Author;
        type Cid = St::Cid;
        type Record = Set<members::record>;
        type Uri = St::Uri;
    }
    ///State transition - sets the `uri` field to Set
    pub struct SetUri<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetUri<St> {}
    impl<St: State> State for SetUri<St> {
        type Author = St::Author;
        type Cid = St::Cid;
        type Record = St::Record;
        type Uri = Set<members::uri>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `author` field
        pub struct author(());
        ///Marker type for the `cid` field
        pub struct cid(());
        ///Marker type for the `record` field
        pub struct record(());
        ///Marker type for the `uri` field
        pub struct uri(());
    }
}

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

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

impl<S: BosStr> TrackView<S> {
    /// Create a new builder for this type
    pub fn builder() -> TrackViewBuilder<track_view_state::Empty, S> {
        TrackViewBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> TrackViewBuilder<St, S>
where
    St: track_view_state::State,
    St::Author: track_view_state::IsUnset,
{
    /// Set the `author` field (required)
    pub fn author(
        mut self,
        value: impl Into<ProfileViewBasic<S>>,
    ) -> TrackViewBuilder<track_view_state::SetAuthor<St>, S> {
        self._fields.0 = Option::Some(value.into());
        TrackViewBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> TrackViewBuilder<St, S>
where
    St: track_view_state::State,
    St::Cid: track_view_state::IsUnset,
{
    /// Set the `cid` field (required)
    pub fn cid(
        mut self,
        value: impl Into<Cid<S>>,
    ) -> TrackViewBuilder<track_view_state::SetCid<St>, S> {
        self._fields.1 = Option::Some(value.into());
        TrackViewBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> TrackViewBuilder<St, S>
where
    St: track_view_state::State,
    St::Record: track_view_state::IsUnset,
{
    /// Set the `record` field (required)
    pub fn record(
        mut self,
        value: impl Into<Data<S>>,
    ) -> TrackViewBuilder<track_view_state::SetRecord<St>, S> {
        self._fields.2 = Option::Some(value.into());
        TrackViewBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> TrackViewBuilder<St, S>
where
    St: track_view_state::State,
    St::Uri: track_view_state::IsUnset,
{
    /// Set the `uri` field (required)
    pub fn uri(
        mut self,
        value: impl Into<AtUri<S>>,
    ) -> TrackViewBuilder<track_view_state::SetUri<St>, S> {
        self._fields.3 = Option::Some(value.into());
        TrackViewBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> TrackViewBuilder<St, S>
where
    St: track_view_state::State,
    St::Author: track_view_state::IsSet,
    St::Cid: track_view_state::IsSet,
    St::Record: track_view_state::IsSet,
    St::Uri: track_view_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> TrackView<S> {
        TrackView {
            author: self._fields.0.unwrap(),
            cid: self._fields.1.unwrap(),
            record: self._fields.2.unwrap(),
            uri: self._fields.3.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>>,
    ) -> TrackView<S> {
        TrackView {
            author: self._fields.0.unwrap(),
            cid: self._fields.1.unwrap(),
            record: self._fields.2.unwrap(),
            uri: self._fields.3.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}