// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.stream.media.viewCount
//
// 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, 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};
use crate::com_atproto::repo::strong_ref::StrongRef;
use crate::place_stream::media::view_count;
/// A streamplace node's report of view counts for one place.stream.video over a closed time window. Published in the reporting node's server repo (not the streamer's), so a video served by multiple nodes accumulates multiple records — consumers are expected to sum across trusted reporters. The rkey is conventionally `<windowStart-as-tid>-<video-rkey>` so re-running the aggregator over the same window is idempotent. Counts represent the reporting node's best effort given the data it has; the `tracks` array carries the objective byte / duration totals it observed.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "place.stream.media.viewCount",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct ViewCount<S: BosStr = DefaultStr> {
    ///Number of distinct sessions the reporting node observed as a view over [windowStart, windowEnd).
    pub count: i64,
    ///When the reporting node ran this aggregation. Useful for ordering successive reports.
    pub indexed_at: Datetime,
    ///Per-track totals of bytes + playback duration the reporting node served over the window. Each entry references the place.stream.media.track record whose bytes were transferred, so user-contributed tracks (transcripts, transcodes published by other accounts) attribute naturally to their own records.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tracks: Option<Vec<view_count::TrackUsage<S>>>,
    ///AT-URI of the place.stream.video this count is for.
    pub video: AtUri<S>,
    ///Exclusive upper bound of the aggregation window.
    pub window_end: Datetime,
    ///Inclusive lower bound of the aggregation window.
    pub window_start: 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 ViewCountGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: ViewCount<S>,
}

/// One row of the tracks array: bytes + duration transferred for a single place.stream.media.track record over the window.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct TrackUsage<S: BosStr = DefaultStr> {
    ///Total bytes served from this track over the window. Sum across attributed segment_requests' Range intersections with the track's segment offsets in the metafile.
    pub bytes: i64,
    ///Total playback duration served from this track, in milliseconds. Per HLS segment in the range: (overlap bytes / segment bytes) * segment duration, so partial-segment fetches credit a proportional share of duration.
    pub duration_ms: i64,
    ///Strong reference to the place.stream.media.track record whose bytes were transferred.
    pub track: StrongRef<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

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

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

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

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

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

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

impl<S: BosStr> LexiconSchema for ViewCount<S> {
    fn nsid() -> &'static str {
        "place.stream.media.viewCount"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_media_viewCount()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.count;
            if *value < 0i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("count"),
                    min: 0i64,
                    actual: *value,
                });
            }
        }
        Ok(())
    }
}

impl<S: BosStr> LexiconSchema for TrackUsage<S> {
    fn nsid() -> &'static str {
        "place.stream.media.viewCount"
    }
    fn def_name() -> &'static str {
        "trackUsage"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_media_viewCount()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.bytes;
            if *value < 0i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("bytes"),
                    min: 0i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.duration_ms;
            if *value < 0i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("duration_ms"),
                    min: 0i64,
                    actual: *value,
                });
            }
        }
        Ok(())
    }
}

pub mod view_count_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 Count;
        type IndexedAt;
        type Video;
        type WindowEnd;
        type WindowStart;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Count = Unset;
        type IndexedAt = Unset;
        type Video = Unset;
        type WindowEnd = Unset;
        type WindowStart = Unset;
    }
    ///State transition - sets the `count` field to Set
    pub struct SetCount<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetCount<St> {}
    impl<St: State> State for SetCount<St> {
        type Count = Set<members::count>;
        type IndexedAt = St::IndexedAt;
        type Video = St::Video;
        type WindowEnd = St::WindowEnd;
        type WindowStart = St::WindowStart;
    }
    ///State transition - sets the `indexed_at` field to Set
    pub struct SetIndexedAt<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetIndexedAt<St> {}
    impl<St: State> State for SetIndexedAt<St> {
        type Count = St::Count;
        type IndexedAt = Set<members::indexed_at>;
        type Video = St::Video;
        type WindowEnd = St::WindowEnd;
        type WindowStart = St::WindowStart;
    }
    ///State transition - sets the `video` field to Set
    pub struct SetVideo<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetVideo<St> {}
    impl<St: State> State for SetVideo<St> {
        type Count = St::Count;
        type IndexedAt = St::IndexedAt;
        type Video = Set<members::video>;
        type WindowEnd = St::WindowEnd;
        type WindowStart = St::WindowStart;
    }
    ///State transition - sets the `window_end` field to Set
    pub struct SetWindowEnd<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetWindowEnd<St> {}
    impl<St: State> State for SetWindowEnd<St> {
        type Count = St::Count;
        type IndexedAt = St::IndexedAt;
        type Video = St::Video;
        type WindowEnd = Set<members::window_end>;
        type WindowStart = St::WindowStart;
    }
    ///State transition - sets the `window_start` field to Set
    pub struct SetWindowStart<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetWindowStart<St> {}
    impl<St: State> State for SetWindowStart<St> {
        type Count = St::Count;
        type IndexedAt = St::IndexedAt;
        type Video = St::Video;
        type WindowEnd = St::WindowEnd;
        type WindowStart = Set<members::window_start>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `count` field
        pub struct count(());
        ///Marker type for the `indexed_at` field
        pub struct indexed_at(());
        ///Marker type for the `video` field
        pub struct video(());
        ///Marker type for the `window_end` field
        pub struct window_end(());
        ///Marker type for the `window_start` field
        pub struct window_start(());
    }
}

/// Builder for constructing an instance of this type.
pub struct ViewCountBuilder<St: view_count_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (
        Option<i64>,
        Option<Datetime>,
        Option<Vec<view_count::TrackUsage<S>>>,
        Option<AtUri<S>>,
        Option<Datetime>,
        Option<Datetime>,
    ),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> ViewCount<S> {
    /// Create a new builder for this type
    pub fn builder() -> ViewCountBuilder<view_count_state::Empty, S> {
        ViewCountBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> ViewCountBuilder<St, S>
where
    St: view_count_state::State,
    St::Count: view_count_state::IsUnset,
{
    /// Set the `count` field (required)
    pub fn count(
        mut self,
        value: impl Into<i64>,
    ) -> ViewCountBuilder<view_count_state::SetCount<St>, S> {
        self._fields.0 = Option::Some(value.into());
        ViewCountBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ViewCountBuilder<St, S>
where
    St: view_count_state::State,
    St::IndexedAt: view_count_state::IsUnset,
{
    /// Set the `indexedAt` field (required)
    pub fn indexed_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> ViewCountBuilder<view_count_state::SetIndexedAt<St>, S> {
        self._fields.1 = Option::Some(value.into());
        ViewCountBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: view_count_state::State, S: BosStr> ViewCountBuilder<St, S> {
    /// Set the `tracks` field (optional)
    pub fn tracks(
        mut self,
        value: impl Into<Option<Vec<view_count::TrackUsage<S>>>>,
    ) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `tracks` field to an Option value (optional)
    pub fn maybe_tracks(
        mut self,
        value: Option<Vec<view_count::TrackUsage<S>>>,
    ) -> Self {
        self._fields.2 = value;
        self
    }
}

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

impl<St, S: BosStr> ViewCountBuilder<St, S>
where
    St: view_count_state::State,
    St::WindowEnd: view_count_state::IsUnset,
{
    /// Set the `windowEnd` field (required)
    pub fn window_end(
        mut self,
        value: impl Into<Datetime>,
    ) -> ViewCountBuilder<view_count_state::SetWindowEnd<St>, S> {
        self._fields.4 = Option::Some(value.into());
        ViewCountBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ViewCountBuilder<St, S>
where
    St: view_count_state::State,
    St::WindowStart: view_count_state::IsUnset,
{
    /// Set the `windowStart` field (required)
    pub fn window_start(
        mut self,
        value: impl Into<Datetime>,
    ) -> ViewCountBuilder<view_count_state::SetWindowStart<St>, S> {
        self._fields.5 = Option::Some(value.into());
        ViewCountBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ViewCountBuilder<St, S>
where
    St: view_count_state::State,
    St::Count: view_count_state::IsSet,
    St::IndexedAt: view_count_state::IsSet,
    St::Video: view_count_state::IsSet,
    St::WindowEnd: view_count_state::IsSet,
    St::WindowStart: view_count_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> ViewCount<S> {
        ViewCount {
            count: self._fields.0.unwrap(),
            indexed_at: self._fields.1.unwrap(),
            tracks: self._fields.2,
            video: self._fields.3.unwrap(),
            window_end: self._fields.4.unwrap(),
            window_start: 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>>,
    ) -> ViewCount<S> {
        ViewCount {
            count: self._fields.0.unwrap(),
            indexed_at: self._fields.1.unwrap(),
            tracks: self._fields.2,
            video: self._fields.3.unwrap(),
            window_end: self._fields.4.unwrap(),
            window_start: self._fields.5.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_place_stream_media_viewCount() -> 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.viewCount"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static(
                            "A streamplace node's report of view counts for one place.stream.video over a closed time window. Published in the reporting node's server repo (not the streamer's), so a video served by multiple nodes accumulates multiple records — consumers are expected to sum across trusted reporters. The rkey is conventionally `<windowStart-as-tid>-<video-rkey>` so re-running the aggregator over the same window is idempotent. Counts represent the reporting node's best effort given the data it has; the `tracks` array carries the objective byte / duration totals it observed.",
                        ),
                    ),
                    key: Some(CowStr::new_static("any")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("video"), SmolStr::new_static("count"),
                                SmolStr::new_static("windowStart"),
                                SmolStr::new_static("windowEnd"),
                                SmolStr::new_static("indexedAt")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("count"),
                                LexObjectProperty::Integer(LexInteger {
                                    minimum: Some(0i64),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("indexedAt"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "When the reporting node ran this aggregation. Useful for ordering successive reports.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("tracks"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static(
                                            "Per-track totals of bytes + playback duration the reporting node served over the window. Each entry references the place.stream.media.track record whose bytes were transferred, so user-contributed tracks (transcripts, transcodes published by other accounts) attribute naturally to their own records.",
                                        ),
                                    ),
                                    items: LexArrayItem::Ref(LexRef {
                                        r#ref: CowStr::new_static("#trackUsage"),
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("video"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "AT-URI of the place.stream.video this count is for.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::AtUri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("windowEnd"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Exclusive upper bound of the aggregation window.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("windowStart"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Inclusive lower bound of the aggregation window.",
                                        ),
                                    ),
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("trackUsage"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "One row of the tracks array: bytes + duration transferred for a single place.stream.media.track record over the window.",
                        ),
                    ),
                    required: Some(
                        vec![
                            SmolStr::new_static("track"), SmolStr::new_static("bytes"),
                            SmolStr::new_static("durationMs")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("bytes"),
                            LexObjectProperty::Integer(LexInteger {
                                minimum: Some(0i64),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("durationMs"),
                            LexObjectProperty::Integer(LexInteger {
                                minimum: Some(0i64),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("track"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static("com.atproto.repo.strongRef"),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

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

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

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

impl<S: BosStr> TrackUsage<S> {
    /// Create a new builder for this type
    pub fn builder() -> TrackUsageBuilder<track_usage_state::Empty, S> {
        TrackUsageBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> TrackUsageBuilder<St, S>
where
    St: track_usage_state::State,
    St::Bytes: track_usage_state::IsUnset,
{
    /// Set the `bytes` field (required)
    pub fn bytes(
        mut self,
        value: impl Into<i64>,
    ) -> TrackUsageBuilder<track_usage_state::SetBytes<St>, S> {
        self._fields.0 = Option::Some(value.into());
        TrackUsageBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> TrackUsageBuilder<St, S>
where
    St: track_usage_state::State,
    St::DurationMs: track_usage_state::IsUnset,
{
    /// Set the `durationMs` field (required)
    pub fn duration_ms(
        mut self,
        value: impl Into<i64>,
    ) -> TrackUsageBuilder<track_usage_state::SetDurationMs<St>, S> {
        self._fields.1 = Option::Some(value.into());
        TrackUsageBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

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

impl<St, S: BosStr> TrackUsageBuilder<St, S>
where
    St: track_usage_state::State,
    St::Bytes: track_usage_state::IsSet,
    St::DurationMs: track_usage_state::IsSet,
    St::Track: track_usage_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> TrackUsage<S> {
        TrackUsage {
            bytes: self._fields.0.unwrap(),
            duration_ms: self._fields.1.unwrap(),
            track: 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>>,
    ) -> TrackUsage<S> {
        TrackUsage {
            bytes: self._fields.0.unwrap(),
            duration_ms: self._fields.1.unwrap(),
            track: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}