// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: tools.ozone.safelink.queryEvents
//
// 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_derive::{IntoStatic, lexicon};
use serde::{Serialize, Deserialize};
use crate::tools_ozone::safelink::Event;

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct QueryEvents<'a> {
    ///Cursor for pagination
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cursor: Option<CowStr<'a>>,
    ///Maximum number of results to return  Defaults to `50`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "_default_query_events_limit")]
    pub limit: Option<i64>,
    ///Filter by pattern type
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub pattern_type: Option<CowStr<'a>>,
    ///Sort direction
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub sort_direction: Option<QueryEventsSortDirection<'a>>,
    ///Filter by specific URLs or domains
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub urls: Option<Vec<CowStr<'a>>>,
}

/// Sort direction

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum QueryEventsSortDirection<'a> {
    Asc,
    Desc,
    Other(CowStr<'a>),
}

impl<'a> QueryEventsSortDirection<'a> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Asc => "asc",
            Self::Desc => "desc",
            Self::Other(s) => s.as_ref(),
        }
    }
}

impl<'a> From<&'a str> for QueryEventsSortDirection<'a> {
    fn from(s: &'a str) -> Self {
        match s {
            "asc" => Self::Asc,
            "desc" => Self::Desc,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> From<String> for QueryEventsSortDirection<'a> {
    fn from(s: String) -> Self {
        match s.as_str() {
            "asc" => Self::Asc,
            "desc" => Self::Desc,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> core::fmt::Display for QueryEventsSortDirection<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<'a> AsRef<str> for QueryEventsSortDirection<'a> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<'a> serde::Serialize for QueryEventsSortDirection<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de, 'a> serde::Deserialize<'de> for QueryEventsSortDirection<'a>
where
    'de: 'a,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = <&'de str>::deserialize(deserializer)?;
        Ok(Self::from(s))
    }
}

impl<'a> Default for QueryEventsSortDirection<'a> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl jacquard_common::IntoStatic for QueryEventsSortDirection<'_> {
    type Output = QueryEventsSortDirection<'static>;
    fn into_static(self) -> Self::Output {
        match self {
            QueryEventsSortDirection::Asc => QueryEventsSortDirection::Asc,
            QueryEventsSortDirection::Desc => QueryEventsSortDirection::Desc,
            QueryEventsSortDirection::Other(v) => {
                QueryEventsSortDirection::Other(v.into_static())
            }
        }
    }
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct QueryEventsOutput<'a> {
    ///Next cursor for pagination. Only present if there are more results.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cursor: Option<CowStr<'a>>,
    #[serde(borrow)]
    pub events: Vec<Event<'a>>,
}

/// Response type for tools.ozone.safelink.queryEvents
pub struct QueryEventsResponse;
impl jacquard_common::xrpc::XrpcResp for QueryEventsResponse {
    const NSID: &'static str = "tools.ozone.safelink.queryEvents";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = QueryEventsOutput<'de>;
    type Err<'de> = jacquard_common::xrpc::GenericError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for QueryEvents<'a> {
    const NSID: &'static str = "tools.ozone.safelink.queryEvents";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = QueryEventsResponse;
}

/// Endpoint type for tools.ozone.safelink.queryEvents
pub struct QueryEventsRequest;
impl jacquard_common::xrpc::XrpcEndpoint for QueryEventsRequest {
    const PATH: &'static str = "/xrpc/tools.ozone.safelink.queryEvents";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = QueryEvents<'de>;
    type Response = QueryEventsResponse;
}

fn _default_query_events_limit() -> Option<i64> {
    Some(50i64)
}