// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.stream.media.publishVideo
//
// 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};
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::string::{AtUri, Cid};
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::place_stream::video::Video;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct PublishVideo<S: BosStr = DefaultStr> {
    ///A place.stream.video record. The server overrides `source` and `durationMs` from the processed upload, and fills in `thumb` with a generated thumbnail when the supplied record has none.
    pub record: Video<S>,
    ///The upload ID returned by place.stream.media.createUpload. Its processing must be complete (status 'done').
    pub upload_id: S,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct PublishVideoOutput<S: BosStr = DefaultStr> {
    ///CID of the created place.stream.video record.
    pub cid: Cid<S>,
    ///AT-URI of the created place.stream.video record.
    pub uri: AtUri<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[derive(
    Serialize,
    Deserialize,
    Debug,
    Clone,
    PartialEq,
    Eq,
    thiserror::Error,
    miette::Diagnostic
)]

#[serde(tag = "error", content = "message")]
pub enum PublishVideoError {
    /// No upload with the given ID belongs to the authenticated user.
    #[serde(rename = "UploadNotFound")]
    UploadNotFound(Option<SmolStr>),
    /// The upload has not finished processing, so its tracks aren't available yet.
    #[serde(rename = "UploadNotReady")]
    UploadNotReady(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for PublishVideoError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::UploadNotFound(msg) => {
                write!(f, "UploadNotFound")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::UploadNotReady(msg) => {
                write!(f, "UploadNotReady")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::Other { error, message } => {
                write!(f, "{}", error)?;
                if let Some(msg) = message {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
        }
    }
}

/** Response marker for the `place.stream.media.publishVideo` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `PublishVideoOutput<S>` for this endpoint.*/
pub struct PublishVideoResponse;
impl jacquard_common::xrpc::XrpcResp for PublishVideoResponse {
    const NSID: &'static str = "place.stream.media.publishVideo";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = PublishVideoOutput<S>;
    type Err = PublishVideoError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for PublishVideo<S> {
    const NSID: &'static str = "place.stream.media.publishVideo";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = PublishVideoResponse;
}

/** Endpoint marker for the `place.stream.media.publishVideo` procedure.

Path: `/xrpc/place.stream.media.publishVideo`. The request payload type is `PublishVideo<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
pub struct PublishVideoRequest;
impl jacquard_common::xrpc::XrpcEndpoint for PublishVideoRequest {
    const PATH: &'static str = "/xrpc/place.stream.media.publishVideo";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<S: BosStr> = PublishVideo<S>;
    type Response = PublishVideoResponse;
}

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

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

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

impl<S: BosStr> PublishVideo<S> {
    /// Create a new builder for this type
    pub fn builder() -> PublishVideoBuilder<publish_video_state::Empty, S> {
        PublishVideoBuilder::builder()
    }
}

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

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

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

impl<St, S: BosStr> PublishVideoBuilder<St, S>
where
    St: publish_video_state::State,
    St::UploadId: publish_video_state::IsUnset,
{
    /// Set the `uploadId` field (required)
    pub fn upload_id(
        mut self,
        value: impl Into<S>,
    ) -> PublishVideoBuilder<publish_video_state::SetUploadId<St>, S> {
        self._fields.1 = Option::Some(value.into());
        PublishVideoBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> PublishVideoBuilder<St, S>
where
    St: publish_video_state::State,
    St::Record: publish_video_state::IsSet,
    St::UploadId: publish_video_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> PublishVideo<S> {
        PublishVideo {
            record: self._fields.0.unwrap(),
            upload_id: self._fields.1.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>>,
    ) -> PublishVideo<S> {
        PublishVideo {
            record: self._fields.0.unwrap(),
            upload_id: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}