// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: app.bsky.unspecced.getPostThreadV2
//
// 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::string::AtUri;
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, open_union};
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::feed::ThreadgateView;
use crate::app_bsky::unspecced::ThreadItemBlocked;
use crate::app_bsky::unspecced::ThreadItemNoUnauthenticated;
use crate::app_bsky::unspecced::ThreadItemNotFound;
use crate::app_bsky::unspecced::ThreadItemPost;
use crate::app_bsky::unspecced::get_post_thread_v2;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct GetPostThreadV2<S: BosStr = DefaultStr> {
    ///  Defaults to `true`.
    #[serde(default = "_default_above")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub above: Option<bool>,
    pub anchor: AtUri<S>,
    /// Defaults to `6`. Min: 0. Max: 20.
    #[serde(default = "_default_below")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub below: Option<i64>,
    /// Defaults to `10`. Min: 0. Max: 100.
    #[serde(default = "_default_branching_factor")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branching_factor: Option<i64>,
    /// Defaults to `"oldest"`.
    #[serde(default = "_default_sort")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort: Option<S>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct GetPostThreadV2Output<S: BosStr = DefaultStr> {
    ///Whether this thread has additional replies. If true, a call can be made to the `getPostThreadOtherV2` endpoint to retrieve them.
    pub has_other_replies: bool,
    ///A flat list of thread items. The depth of each item is indicated by the depth property inside the item.
    pub thread: Vec<get_post_thread_v2::ThreadItem<S>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub threadgate: Option<ThreadgateView<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 ThreadItem<S: BosStr = DefaultStr> {
    ///The nesting level of this item in the thread. Depth 0 means the anchor item. Items above have negative depths, items below have positive depths.
    pub depth: i64,
    pub uri: AtUri<S>,
    pub value: ThreadItemValue<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[open_union]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(tag = "$type", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub enum ThreadItemValue<S: BosStr = DefaultStr> {
    #[serde(rename = "app.bsky.unspecced.defs#threadItemPost")]
    ThreadItemPost(Box<ThreadItemPost<S>>),
    #[serde(rename = "app.bsky.unspecced.defs#threadItemNoUnauthenticated")]
    ThreadItemNoUnauthenticated(Box<ThreadItemNoUnauthenticated<S>>),
    #[serde(rename = "app.bsky.unspecced.defs#threadItemNotFound")]
    ThreadItemNotFound(Box<ThreadItemNotFound<S>>),
    #[serde(rename = "app.bsky.unspecced.defs#threadItemBlocked")]
    ThreadItemBlocked(Box<ThreadItemBlocked<S>>),
}

/** Response marker for the `app.bsky.unspecced.getPostThreadV2` query.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `GetPostThreadV2Output<S>` for this endpoint.*/
pub struct GetPostThreadV2Response;
impl jacquard_common::xrpc::XrpcResp for GetPostThreadV2Response {
    const NSID: &'static str = "app.bsky.unspecced.getPostThreadV2";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = GetPostThreadV2Output<S>;
    type Err = jacquard_common::xrpc::GenericError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for GetPostThreadV2<S> {
    const NSID: &'static str = "app.bsky.unspecced.getPostThreadV2";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Response = GetPostThreadV2Response;
}

/** Endpoint marker for the `app.bsky.unspecced.getPostThreadV2` query.

Path: `/xrpc/app.bsky.unspecced.getPostThreadV2`. The request payload type is `GetPostThreadV2<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
pub struct GetPostThreadV2Request;
impl jacquard_common::xrpc::XrpcEndpoint for GetPostThreadV2Request {
    const PATH: &'static str = "/xrpc/app.bsky.unspecced.getPostThreadV2";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Request<S: BosStr> = GetPostThreadV2<S>;
    type Response = GetPostThreadV2Response;
}

impl<S: BosStr> LexiconSchema for ThreadItem<S> {
    fn nsid() -> &'static str {
        "app.bsky.unspecced.getPostThreadV2"
    }
    fn def_name() -> &'static str {
        "threadItem"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_app_bsky_unspecced_getPostThreadV2()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

fn _default_above() -> Option<bool> {
    Some(true)
}

fn _default_below() -> Option<i64> {
    Some(6i64)
}

fn _default_branching_factor() -> Option<i64> {
    Some(10i64)
}

fn _default_sort<S: jacquard_common::FromStaticStr>() -> Option<S> {
    Some(S::from_static("oldest"))
}

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

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

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

impl<S: BosStr> GetPostThreadV2<S> {
    /// Create a new builder for this type
    pub fn builder() -> GetPostThreadV2Builder<get_post_thread_v2_state::Empty, S> {
        GetPostThreadV2Builder::builder()
    }
}

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

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

impl<St: get_post_thread_v2_state::State, S: BosStr> GetPostThreadV2Builder<St, S> {
    /// Set the `above` field (optional)
    pub fn above(mut self, value: impl Into<Option<bool>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `above` field to an Option value (optional)
    pub fn maybe_above(mut self, value: Option<bool>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<St, S: BosStr> GetPostThreadV2Builder<St, S>
where
    St: get_post_thread_v2_state::State,
    St::Anchor: get_post_thread_v2_state::IsUnset,
{
    /// Set the `anchor` field (required)
    pub fn anchor(
        mut self,
        value: impl Into<AtUri<S>>,
    ) -> GetPostThreadV2Builder<get_post_thread_v2_state::SetAnchor<St>, S> {
        self._fields.1 = Option::Some(value.into());
        GetPostThreadV2Builder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: get_post_thread_v2_state::State, S: BosStr> GetPostThreadV2Builder<St, S> {
    /// Set the `below` field (optional)
    pub fn below(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `below` field to an Option value (optional)
    pub fn maybe_below(mut self, value: Option<i64>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<St: get_post_thread_v2_state::State, S: BosStr> GetPostThreadV2Builder<St, S> {
    /// Set the `branchingFactor` field (optional)
    pub fn branching_factor(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `branchingFactor` field to an Option value (optional)
    pub fn maybe_branching_factor(mut self, value: Option<i64>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<St: get_post_thread_v2_state::State, S: BosStr> GetPostThreadV2Builder<St, S> {
    /// Set the `sort` field (optional)
    pub fn sort(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `sort` field to an Option value (optional)
    pub fn maybe_sort(mut self, value: Option<S>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<St, S: BosStr> GetPostThreadV2Builder<St, S>
where
    St: get_post_thread_v2_state::State,
    St::Anchor: get_post_thread_v2_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> GetPostThreadV2<S> {
        GetPostThreadV2 {
            above: self._fields.0,
            anchor: self._fields.1.unwrap(),
            below: self._fields.2,
            branching_factor: self._fields.3,
            sort: self._fields.4,
        }
    }
}

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

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

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

impl<S: BosStr> ThreadItem<S> {
    /// Create a new builder for this type
    pub fn builder() -> ThreadItemBuilder<thread_item_state::Empty, S> {
        ThreadItemBuilder::builder()
    }
}

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

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

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

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

impl<St, S: BosStr> ThreadItemBuilder<St, S>
where
    St: thread_item_state::State,
    St::Value: thread_item_state::IsUnset,
{
    /// Set the `value` field (required)
    pub fn value(
        mut self,
        value: impl Into<ThreadItemValue<S>>,
    ) -> ThreadItemBuilder<thread_item_state::SetValue<St>, S> {
        self._fields.2 = Option::Some(value.into());
        ThreadItemBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ThreadItemBuilder<St, S>
where
    St: thread_item_state::State,
    St::Depth: thread_item_state::IsSet,
    St::Uri: thread_item_state::IsSet,
    St::Value: thread_item_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> ThreadItem<S> {
        ThreadItem {
            depth: self._fields.0.unwrap(),
            uri: self._fields.1.unwrap(),
            value: 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>>,
    ) -> ThreadItem<S> {
        ThreadItem {
            depth: self._fields.0.unwrap(),
            uri: self._fields.1.unwrap(),
            value: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_app_bsky_unspecced_getPostThreadV2() -> 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("app.bsky.unspecced.getPostThreadV2"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::XrpcQuery(LexXrpcQuery {
                    parameters: Some(
                        LexXrpcQueryParameter::Params(LexXrpcParameters {
                            required: Some(vec![SmolStr::new_static("anchor")]),
                            properties: {
                                #[allow(unused_mut)]
                                let mut map = BTreeMap::new();
                                map.insert(
                                    SmolStr::new_static("above"),
                                    LexXrpcParametersProperty::Boolean(LexBoolean {
                                        ..Default::default()
                                    }),
                                );
                                map.insert(
                                    SmolStr::new_static("anchor"),
                                    LexXrpcParametersProperty::String(LexString {
                                        description: Some(
                                            CowStr::new_static(
                                                "Reference (AT-URI) to post record. This is the anchor post, and the thread will be built around it. It can be any post in the tree, not necessarily a root post.",
                                            ),
                                        ),
                                        format: Some(LexStringFormat::AtUri),
                                        ..Default::default()
                                    }),
                                );
                                map.insert(
                                    SmolStr::new_static("below"),
                                    LexXrpcParametersProperty::Integer(LexInteger {
                                        ..Default::default()
                                    }),
                                );
                                map.insert(
                                    SmolStr::new_static("branchingFactor"),
                                    LexXrpcParametersProperty::Integer(LexInteger {
                                        ..Default::default()
                                    }),
                                );
                                map.insert(
                                    SmolStr::new_static("sort"),
                                    LexXrpcParametersProperty::String(LexString {
                                        description: Some(
                                            CowStr::new_static("Sorting for the thread replies."),
                                        ),
                                        ..Default::default()
                                    }),
                                );
                                map
                            },
                            ..Default::default()
                        }),
                    ),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("threadItem"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![
                            SmolStr::new_static("uri"), SmolStr::new_static("depth"),
                            SmolStr::new_static("value")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("depth"),
                            LexObjectProperty::Integer(LexInteger {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("uri"),
                            LexObjectProperty::String(LexString {
                                format: Some(LexStringFormat::AtUri),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("value"),
                            LexObjectProperty::Union(LexRefUnion {
                                refs: vec![
                                    CowStr::new_static("app.bsky.unspecced.defs#threadItemPost"),
                                    CowStr::new_static("app.bsky.unspecced.defs#threadItemNoUnauthenticated"),
                                    CowStr::new_static("app.bsky.unspecced.defs#threadItemNotFound"),
                                    CowStr::new_static("app.bsky.unspecced.defs#threadItemBlocked")
                                ],
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}