// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: io.atcr.hold.setStats
//
// 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;
use jacquard_common::types::string::{Did, Datetime};
use jacquard_derive::{IntoStatic, lexicon, open_union};
use serde::{Serialize, Deserialize};

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct SetStats<'a> {
    ///RFC3339 timestamp of last pull
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_pull: Option<Datetime>,
    ///RFC3339 timestamp of last push
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_push: Option<Datetime>,
    ///DID of the repository owner
    #[serde(borrow)]
    pub owner_did: Did<'a>,
    ///Absolute pull count to set
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pull_count: Option<i64>,
    ///Absolute push count to set
    #[serde(skip_serializing_if = "Option::is_none")]
    pub push_count: Option<i64>,
    ///Repository name
    #[serde(borrow)]
    pub repository: CowStr<'a>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct SetStatsOutput<'a> {
    ///Whether the stats were successfully updated
    pub success: bool,
}


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

#[serde(tag = "error", content = "message")]
#[serde(bound(deserialize = "'de: 'a"))]
pub enum SetStatsError<'a> {
    #[serde(rename = "InvalidOwner")]
    InvalidOwner(Option<CowStr<'a>>),
    #[serde(rename = "InvalidRepository")]
    InvalidRepository(Option<CowStr<'a>>),
}

impl core::fmt::Display for SetStatsError<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidOwner(msg) => {
                write!(f, "InvalidOwner")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::InvalidRepository(msg) => {
                write!(f, "InvalidRepository")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::Unknown(err) => write!(f, "Unknown error: {:?}", err),
        }
    }
}

/// Response type for io.atcr.hold.setStats
pub struct SetStatsResponse;
impl jacquard_common::xrpc::XrpcResp for SetStatsResponse {
    const NSID: &'static str = "io.atcr.hold.setStats";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = SetStatsOutput<'de>;
    type Err<'de> = SetStatsError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for SetStats<'a> {
    const NSID: &'static str = "io.atcr.hold.setStats";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = SetStatsResponse;
}

/// Endpoint type for io.atcr.hold.setStats
pub struct SetStatsRequest;
impl jacquard_common::xrpc::XrpcEndpoint for SetStatsRequest {
    const PATH: &'static str = "/xrpc/io.atcr.hold.setStats";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = SetStats<'de>;
    type Response = SetStatsResponse;
}

pub mod set_stats_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 Repository;
        type OwnerDid;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Repository = Unset;
        type OwnerDid = Unset;
    }
    ///State transition - sets the `repository` field to Set
    pub struct SetRepository<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetRepository<S> {}
    impl<S: State> State for SetRepository<S> {
        type Repository = Set<members::repository>;
        type OwnerDid = S::OwnerDid;
    }
    ///State transition - sets the `owner_did` field to Set
    pub struct SetOwnerDid<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetOwnerDid<S> {}
    impl<S: State> State for SetOwnerDid<S> {
        type Repository = S::Repository;
        type OwnerDid = Set<members::owner_did>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `repository` field
        pub struct repository(());
        ///Marker type for the `owner_did` field
        pub struct owner_did(());
    }
}

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

impl<'a> SetStats<'a> {
    /// Create a new builder for this type
    pub fn new() -> SetStatsBuilder<'a, set_stats_state::Empty> {
        SetStatsBuilder::new()
    }
}

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

impl<'a, S: set_stats_state::State> SetStatsBuilder<'a, S> {
    /// Set the `lastPull` field (optional)
    pub fn last_pull(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `lastPull` field to an Option value (optional)
    pub fn maybe_last_pull(mut self, value: Option<Datetime>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S: set_stats_state::State> SetStatsBuilder<'a, S> {
    /// Set the `lastPush` field (optional)
    pub fn last_push(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `lastPush` field to an Option value (optional)
    pub fn maybe_last_push(mut self, value: Option<Datetime>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S> SetStatsBuilder<'a, S>
where
    S: set_stats_state::State,
    S::OwnerDid: set_stats_state::IsUnset,
{
    /// Set the `ownerDid` field (required)
    pub fn owner_did(
        mut self,
        value: impl Into<Did<'a>>,
    ) -> SetStatsBuilder<'a, set_stats_state::SetOwnerDid<S>> {
        self._fields.2 = Option::Some(value.into());
        SetStatsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: set_stats_state::State> SetStatsBuilder<'a, S> {
    /// Set the `pullCount` field (optional)
    pub fn pull_count(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `pullCount` field to an Option value (optional)
    pub fn maybe_pull_count(mut self, value: Option<i64>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<'a, S: set_stats_state::State> SetStatsBuilder<'a, S> {
    /// Set the `pushCount` field (optional)
    pub fn push_count(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `pushCount` field to an Option value (optional)
    pub fn maybe_push_count(mut self, value: Option<i64>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<'a, S> SetStatsBuilder<'a, S>
where
    S: set_stats_state::State,
    S::Repository: set_stats_state::IsUnset,
{
    /// Set the `repository` field (required)
    pub fn repository(
        mut self,
        value: impl Into<CowStr<'a>>,
    ) -> SetStatsBuilder<'a, set_stats_state::SetRepository<S>> {
        self._fields.5 = Option::Some(value.into());
        SetStatsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> SetStatsBuilder<'a, S>
where
    S: set_stats_state::State,
    S::Repository: set_stats_state::IsSet,
    S::OwnerDid: set_stats_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> SetStats<'a> {
        SetStats {
            last_pull: self._fields.0,
            last_push: self._fields.1,
            owner_did: self._fields.2.unwrap(),
            pull_count: self._fields.3,
            push_count: self._fields.4,
            repository: 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<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> SetStats<'a> {
        SetStats {
            last_pull: self._fields.0,
            last_push: self._fields.1,
            owner_did: self._fields.2.unwrap(),
            pull_count: self._fields.3,
            push_count: self._fields.4,
            repository: self._fields.5.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}