// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: app.bsky.ageassurance.begin
//
// 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::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::app_bsky::ageassurance::State;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Begin<S: BosStr = DefaultStr> {
    ///An ISO 3166-1 alpha-2 code of the user's location.
    pub country_code: S,
    ///The user's email address to receive Age Assurance instructions.
    pub email: S,
    ///The user's preferred language for communication during the Age Assurance process.
    pub language: S,
    ///An optional ISO 3166-2 code of the user's region or state within the country.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region_code: Option<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 BeginOutput<S: BosStr = DefaultStr> {
    #[serde(flatten)]
    pub value: State<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 BeginError {
    #[serde(rename = "InvalidEmail")]
    InvalidEmail(Option<SmolStr>),
    #[serde(rename = "DidTooLong")]
    DidTooLong(Option<SmolStr>),
    #[serde(rename = "InvalidInitiation")]
    InvalidInitiation(Option<SmolStr>),
    #[serde(rename = "RegionNotSupported")]
    RegionNotSupported(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for BeginError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidEmail(msg) => {
                write!(f, "InvalidEmail")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::DidTooLong(msg) => {
                write!(f, "DidTooLong")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::InvalidInitiation(msg) => {
                write!(f, "InvalidInitiation")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::RegionNotSupported(msg) => {
                write!(f, "RegionNotSupported")?;
                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 `app.bsky.ageassurance.begin` procedure.

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

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for Begin<S> {
    const NSID: &'static str = "app.bsky.ageassurance.begin";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = BeginResponse;
}

/** Endpoint marker for the `app.bsky.ageassurance.begin` procedure.

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