// !!!
// WARNING: This file is autogenerated
// Only modify code within MANUAL() sections
// or your changes may be overwritten later!
// !!!

use crate::consumer::device_history::DeviceInfo;
use crate::consumer::sessions::Session;
use crate::consumer::users::User;
use crate::consumer::users::WebAuthnRegistration;
use percent_encoding;
use serde::{Deserialize, Serialize};
use serde_urlencoded;

/// WebAuthnCredential:
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WebAuthnCredential {
    /// credential_id: The unique, public ID of the WebAuthn credential.
    pub credential_id: String,
    /// webauthn_registration_id: The unique ID for the Passkey or WebAuthn registration.
    pub webauthn_registration_id: String,
    /// type_: The type of the WebAuthn credential. Examples include `public-key`.
    #[serde(rename = "type")]
    pub type_: String,
    /// public_key: The public key for the WebAuthn credential in base64 format.
    pub public_key: String,
}
/// AuthenticateRequest: Request type for `WebAuthn.authenticate`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct AuthenticateRequest {
    /// public_key_credential: The response of the
    /// [navigator.credentials.create()](https://www.w3.org/TR/webauthn-2/#sctn-createCredential).
    pub public_key_credential: String,
    /// session_token: The `session_token` associated with a User's existing Session.
    pub session_token: std::option::Option<String>,
    /// session_duration_minutes: Set the session lifetime to be this many minutes from now. This will start a
    /// new session if one doesn't already exist,
    ///   returning both an opaque `session_token` and `session_jwt` for this session. Remember that the
    /// `session_jwt` will have a fixed lifetime of
    ///   five minutes regardless of the underlying session duration, and will need to be refreshed over time.
    ///
    ///   This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).
    ///
    ///   If a `session_token` or `session_jwt` is provided then a successful authentication will continue to
    /// extend the session this many minutes.
    ///
    ///   If the `session_duration_minutes` parameter is not specified, a Stytch session will not be created.
    pub session_duration_minutes: std::option::Option<i32>,
    /// session_jwt: The `session_jwt` associated with a User's existing Session.
    pub session_jwt: std::option::Option<String>,
    /// session_custom_claims: Add a custom claims map to the Session being authenticated. Claims are only
    /// created if a Session is initialized by providing a value in `session_duration_minutes`. Claims will be
    /// included on the Session object and in the JWT. To update a key in an existing Session, supply a new
    /// value. To delete a key, supply a null value.
    ///
    ///   Custom claims made with reserved claims ("iss", "sub", "aud", "exp", "nbf", "iat", "jti") will be
    /// ignored. Total custom claims size cannot exceed four kilobytes.
    pub session_custom_claims: std::option::Option<serde_json::Value>,
    /// telemetry_id: If the `telemetry_id` is passed, as part of this request, Stytch will call the
    /// [Fingerprint Lookup API](https://stytch.com/docs/fraud/api/fingerprint-lookup) and store the associated
    /// fingerprints and IPGEO information for the User. Your workspace must be enabled for Device
    /// Fingerprinting to use this feature.
    pub telemetry_id: std::option::Option<String>,
}
/// AuthenticateResponse: Response type for `WebAuthn.authenticate`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AuthenticateResponse {
    /// request_id: Globally unique UUID that is returned with every API call. This value is important to log
    /// for debugging purposes; we may ask for this value to help identify a specific API call when helping you
    /// debug an issue.
    pub request_id: String,
    /// user_id: The unique ID of the affected User.
    pub user_id: String,
    /// webauthn_registration_id: The unique ID for the Passkey or WebAuthn registration.
    pub webauthn_registration_id: String,
    /// session_token: A secret token for a given Stytch Session.
    pub session_token: String,
    /// session_jwt: The JSON Web Token (JWT) for a given Stytch Session.
    pub session_jwt: String,
    /// user: The `user` object affected by this API call. See the
    /// [Get user endpoint](https://stytch.com/docs/api/get-user) for complete response field details.
    pub user: User,
    /// status_code: The HTTP status code of the response. Stytch follows standard HTTP response status code
    /// patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX
    /// are server errors.
    #[serde(with = "http_serde::status_code")]
    pub status_code: http::StatusCode,
    /// session: If you initiate a Session, by including `session_duration_minutes` in your authenticate call,
    /// you'll receive a full Session object in the response.
    ///
    ///   See [Session object](https://stytch.com/docs/api/session-object) for complete response fields.
    ///
    pub session: std::option::Option<Session>,
    /// user_device: If a valid `telemetry_id` was passed in the request and the
    /// [Fingerprint Lookup API](https://stytch.com/docs/fraud/api/fingerprint-lookup) returned results, the
    /// `user_device` response field will contain information about the user's device attributes.
    pub user_device: std::option::Option<DeviceInfo>,
}
/// AuthenticateStartRequest: Request type for `WebAuthn.authenticate_start`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct AuthenticateStartRequest {
    /// domain: The domain for Passkeys or WebAuthn. Defaults to `window.location.hostname`.
    pub domain: String,
    /// user_id: The `user_id` of an active user the Passkey or WebAuthn registration should be tied to. You may
    /// use an `external_id` here if one is set for the user.
    pub user_id: std::option::Option<String>,
    /// return_passkey_credential_options: If true, the `public_key_credential_creation_options` returned will
    /// be optimized for Passkeys with `userVerification` set to `"preferred"`.
    ///
    pub return_passkey_credential_options: std::option::Option<bool>,
}
/// AuthenticateStartResponse: Response type for `WebAuthn.authenticate_start`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AuthenticateStartResponse {
    /// request_id: Globally unique UUID that is returned with every API call. This value is important to log
    /// for debugging purposes; we may ask for this value to help identify a specific API call when helping you
    /// debug an issue.
    pub request_id: String,
    /// user_id: The unique ID of the affected User.
    pub user_id: String,
    /// public_key_credential_request_options: Options used for Passkey or WebAuthn authentication.
    pub public_key_credential_request_options: String,
    /// status_code: The HTTP status code of the response. Stytch follows standard HTTP response status code
    /// patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX
    /// are server errors.
    #[serde(with = "http_serde::status_code")]
    pub status_code: http::StatusCode,
}
/// ListCredentialsRequest: Request type for `WebAuthn.list_credentials`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ListCredentialsRequest {
    /// user_id: The `user_id` of an active user the Passkey or WebAuthn registration should be tied to.
    pub user_id: String,
    /// domain: The domain for Passkeys or WebAuthn. Defaults to `window.location.hostname`.
    pub domain: String,
}
/// ListCredentialsResponse: Response type for `WebAuthn.list_credentials`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListCredentialsResponse {
    /// credentials: A list of WebAuthn credential objects.
    pub credentials: std::vec::Vec<WebAuthnCredential>,
    /// request_id: Globally unique UUID that is returned with every API call. This value is important to log
    /// for debugging purposes; we may ask for this value to help identify a specific API call when helping you
    /// debug an issue.
    pub request_id: String,
    /// status_code: The HTTP status code of the response. Stytch follows standard HTTP response status code
    /// patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX
    /// are server errors.
    #[serde(with = "http_serde::status_code")]
    pub status_code: http::StatusCode,
}
/// RegisterRequest: Request type for `WebAuthn.register`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct RegisterRequest {
    /// user_id: The `user_id` of an active user the Passkey or WebAuthn registration should be tied to. You may
    /// use an `external_id` here if one is set for the user.
    pub user_id: String,
    /// public_key_credential: The response of the
    /// [navigator.credentials.create()](https://www.w3.org/TR/webauthn-2/#sctn-createCredential).
    pub public_key_credential: String,
    /// session_token: The `session_token` associated with a User's existing Session.
    pub session_token: std::option::Option<String>,
    /// session_duration_minutes: Set the session lifetime to be this many minutes from now. This will start a
    /// new session if one doesn't already exist,
    ///   returning both an opaque `session_token` and `session_jwt` for this session. Remember that the
    /// `session_jwt` will have a fixed lifetime of
    ///   five minutes regardless of the underlying session duration, and will need to be refreshed over time.
    ///
    ///   This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).
    ///
    ///   If a `session_token` or `session_jwt` is provided then a successful authentication will continue to
    /// extend the session this many minutes.
    ///
    ///   If the `session_duration_minutes` parameter is not specified, a Stytch session will not be created.
    pub session_duration_minutes: std::option::Option<i32>,
    /// session_jwt: The `session_jwt` associated with a User's existing Session.
    pub session_jwt: std::option::Option<String>,
    /// session_custom_claims: Add a custom claims map to the Session being authenticated. Claims are only
    /// created if a Session is initialized by providing a value in `session_duration_minutes`. Claims will be
    /// included on the Session object and in the JWT. To update a key in an existing Session, supply a new
    /// value. To delete a key, supply a null value.
    ///
    ///   Custom claims made with reserved claims ("iss", "sub", "aud", "exp", "nbf", "iat", "jti") will be
    /// ignored. Total custom claims size cannot exceed four kilobytes.
    pub session_custom_claims: std::option::Option<serde_json::Value>,
    /// telemetry_id: If the `telemetry_id` is passed, as part of this request, Stytch will call the
    /// [Fingerprint Lookup API](https://stytch.com/docs/fraud/api/fingerprint-lookup) and store the associated
    /// fingerprints and IPGEO information for the User. Your workspace must be enabled for Device
    /// Fingerprinting to use this feature.
    pub telemetry_id: std::option::Option<String>,
}
/// RegisterResponse: Response type for `WebAuthn.register`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RegisterResponse {
    /// request_id: Globally unique UUID that is returned with every API call. This value is important to log
    /// for debugging purposes; we may ask for this value to help identify a specific API call when helping you
    /// debug an issue.
    pub request_id: String,
    /// user_id: The unique ID of the affected User.
    pub user_id: String,
    /// webauthn_registration_id: The unique ID for the Passkey or WebAuthn registration.
    pub webauthn_registration_id: String,
    /// session_token: A secret token for a given Stytch Session.
    pub session_token: String,
    /// session_jwt: The JSON Web Token (JWT) for a given Stytch Session.
    pub session_jwt: String,
    pub user: User,
    /// status_code: The HTTP status code of the response. Stytch follows standard HTTP response status code
    /// patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX
    /// are server errors.
    #[serde(with = "http_serde::status_code")]
    pub status_code: http::StatusCode,
    /// session: If you initiate a Session, by including `session_duration_minutes` in your authenticate call,
    /// you'll receive a full Session object in the response.
    ///
    ///   See [Session object](https://stytch.com/docs/api/session-object) for complete response fields.
    ///
    pub session: std::option::Option<Session>,
    /// user_device: If a valid `telemetry_id` was passed in the request and the
    /// [Fingerprint Lookup API](https://stytch.com/docs/fraud/api/fingerprint-lookup) returned results, the
    /// `user_device` response field will contain information about the user's device attributes.
    pub user_device: std::option::Option<DeviceInfo>,
}
/// RegisterStartRequest: Request type for `WebAuthn.register_start`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct RegisterStartRequest {
    /// user_id: The `user_id` of an active user the Passkey or WebAuthn registration should be tied to. You may
    /// use an `external_id` here if one is set for the user.
    pub user_id: String,
    /// domain: The domain for Passkeys or WebAuthn. Defaults to `window.location.hostname`.
    pub domain: String,
    /// user_agent: The user agent of the client.
    pub user_agent: std::option::Option<String>,
    /// authenticator_type: The requested authenticator type of the Passkey or WebAuthn device. The two valid
    /// values are platform and cross-platform. If no value passed, we assume both values are allowed.
    pub authenticator_type: std::option::Option<String>,
    /// return_passkey_credential_options: If true, the `public_key_credential_creation_options` returned will
    /// be optimized for Passkeys with `residentKey` set to `"required"` and `userVerification` set to
    /// `"preferred"`.
    ///
    pub return_passkey_credential_options: std::option::Option<bool>,
    pub override_id: std::option::Option<String>,
    pub override_name: std::option::Option<String>,
    pub override_display_name: std::option::Option<String>,
    pub use_base64_url_encoding: std::option::Option<bool>,
}
/// RegisterStartResponse: Response type for `WebAuthn.register_start`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RegisterStartResponse {
    /// request_id: Globally unique UUID that is returned with every API call. This value is important to log
    /// for debugging purposes; we may ask for this value to help identify a specific API call when helping you
    /// debug an issue.
    pub request_id: String,
    /// user_id: The unique ID of the affected User.
    pub user_id: String,
    /// public_key_credential_creation_options: Options used for Passkey or WebAuthn registration.
    pub public_key_credential_creation_options: String,
    /// status_code: The HTTP status code of the response. Stytch follows standard HTTP response status code
    /// patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX
    /// are server errors.
    #[serde(with = "http_serde::status_code")]
    pub status_code: http::StatusCode,
}
/// UpdateRequest: Request type for `WebAuthn.update`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct UpdateRequest {
    /// webauthn_registration_id: Globally unique UUID that identifies a Passkey or WebAuthn registration in the
    /// Stytch API. The `webauthn_registration_id` is used when you need to operate on a specific User's
    /// WebAuthn registration.
    pub webauthn_registration_id: String,
    /// name: The `name` of the WebAuthn registration or Passkey.
    pub name: String,
}
/// UpdateResponse: Response type for `WebAuthn.update`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateResponse {
    /// request_id: Globally unique UUID that is returned with every API call. This value is important to log
    /// for debugging purposes; we may ask for this value to help identify a specific API call when helping you
    /// debug an issue.
    pub request_id: String,
    /// status_code: The HTTP status code of the response. Stytch follows standard HTTP response status code
    /// patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX
    /// are server errors.
    #[serde(with = "http_serde::status_code")]
    pub status_code: http::StatusCode,
    /// webauthn_registration: A Passkey or WebAuthn registration.
    pub webauthn_registration: std::option::Option<WebAuthnRegistration>,
}

pub struct WebAuthn {
    http_client: crate::client::Client,
}

impl WebAuthn {
    pub fn new(http_client: crate::client::Client) -> Self {
        Self {
            http_client: http_client.clone(),
        }
    }

    pub async fn register_start(
        &self,
        body: RegisterStartRequest,
    ) -> crate::Result<RegisterStartResponse> {
        let path = String::from("/v1/webauthn/register/start");
        self.http_client
            .send(crate::Request {
                method: http::Method::POST,
                path,
                body,
            })
            .await
    }
    pub async fn register(&self, body: RegisterRequest) -> crate::Result<RegisterResponse> {
        let path = String::from("/v1/webauthn/register");
        self.http_client
            .send(crate::Request {
                method: http::Method::POST,
                path,
                body,
            })
            .await
    }
    pub async fn authenticate_start(
        &self,
        body: AuthenticateStartRequest,
    ) -> crate::Result<AuthenticateStartResponse> {
        let path = String::from("/v1/webauthn/authenticate/start");
        self.http_client
            .send(crate::Request {
                method: http::Method::POST,
                path,
                body,
            })
            .await
    }
    pub async fn authenticate(
        &self,
        body: AuthenticateRequest,
    ) -> crate::Result<AuthenticateResponse> {
        let path = String::from("/v1/webauthn/authenticate");
        self.http_client
            .send(crate::Request {
                method: http::Method::POST,
                path,
                body,
            })
            .await
    }
    pub async fn update(&self, body: UpdateRequest) -> crate::Result<UpdateResponse> {
        let webauthn_registration_id = percent_encoding::utf8_percent_encode(
            &body.webauthn_registration_id,
            percent_encoding::NON_ALPHANUMERIC,
        )
        .to_string();
        let path = format!("/v1/webauthn/{webauthn_registration_id}");
        self.http_client
            .send(crate::Request {
                method: http::Method::PUT,
                path,
                body,
            })
            .await
    }
    pub async fn list_credentials(
        &self,
        body: ListCredentialsRequest,
    ) -> crate::Result<ListCredentialsResponse> {
        let user_id = percent_encoding::utf8_percent_encode(
            &body.user_id,
            percent_encoding::NON_ALPHANUMERIC,
        )
        .to_string();
        let domain =
            percent_encoding::utf8_percent_encode(&body.domain, percent_encoding::NON_ALPHANUMERIC)
                .to_string();
        let path = format!(
            "/v1/webauthn/credentials/{user_id}/{domain}?{}",
            serde_urlencoded::to_string(body).unwrap()
        );
        self.http_client
            .send(crate::Request {
                method: http::Method::GET,
                path,
                body: serde_json::json!({}),
            })
            .await
    }
}
