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

use crate::b2b::sso::Connection;
use crate::b2b::sso::ConnectionImplicitRoleAssignment;
use crate::b2b::sso::GroupImplicitRoleAssignment;
use crate::b2b::sso::SAMLConnectionImplicitRoleAssignment;
use crate::b2b::sso::SAMLGroupImplicitRoleAssignment;
use percent_encoding;
use serde::{Deserialize, Serialize};

/// CreateConnectionRequest: Request type for `External.create_connection`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct CreateConnectionRequest {
    /// organization_id: Globally unique UUID that identifies a specific Organization. The `organization_id` is
    /// critical to perform operations on an Organization, so be sure to preserve this value. You may also use
    /// the organization_slug or organization_external_id here as a convenience.
    pub organization_id: String,
    /// external_organization_id: Globally unique UUID that identifies a different Organization within your
    /// Project.
    pub external_organization_id: String,
    /// external_connection_id: Globally unique UUID that identifies a specific SSO connection configured for a
    /// different Organization in your Project.
    pub external_connection_id: String,
    /// display_name: A human-readable display name for the connection.
    pub display_name: std::option::Option<String>,
    pub connection_implicit_role_assignments:
        std::option::Option<std::vec::Vec<SAMLConnectionImplicitRoleAssignment>>,
    pub group_implicit_role_assignments:
        std::option::Option<std::vec::Vec<SAMLGroupImplicitRoleAssignment>>,
}
/// CreateConnectionResponse: Response type for `External.create_connection`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CreateConnectionResponse {
    /// 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,
    /// connection: The `External Connection` object affected by this API call. See the
    /// [External Connection Object](https://stytch.com/docs/b2b/api/external-connection-object) for complete
    /// response field details.
    pub connection: std::option::Option<Connection>,
}
/// UpdateConnectionRequest: Request type for `External.update_connection`.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct UpdateConnectionRequest {
    /// organization_id: Globally unique UUID that identifies a specific Organization. The `organization_id` is
    /// critical to perform operations on an Organization, so be sure to preserve this value. You may also use
    /// the organization_slug or organization_external_id here as a convenience.
    pub organization_id: String,
    /// connection_id: Globally unique UUID that identifies a specific External SSO Connection.
    pub connection_id: String,
    /// display_name: A human-readable display name for the connection.
    pub display_name: std::option::Option<String>,
    /// external_connection_implicit_role_assignments: All Members who log in with this External connection will
    /// implicitly receive the specified Roles. See the
    /// [RBAC guide](https://stytch.com/docs/b2b/guides/rbac/role-assignment) for more information about role
    /// assignment. Implicit role assignments are not supported for External connections if the underlying SSO
    /// connection is an OIDC connection.
    pub external_connection_implicit_role_assignments:
        std::option::Option<std::vec::Vec<ConnectionImplicitRoleAssignment>>,
    /// external_group_implicit_role_assignments: Defines the names of the groups
    ///  that grant specific role assignments. For each group-Role pair, if a Member logs in with this external
    /// connection and
    ///  belongs to the specified group, they will be granted the associated Role. See the
    ///  [RBAC guide](https://stytch.com/docs/b2b/guides/rbac/role-assignment) for more information about role
    /// assignment. Before adding any group implicit role assignments to an external connection, you must add a
    /// "groups" key to the underlying SAML connection's
    ///  `attribute_mapping`. Make sure that the SAML connection IdP is configured to correctly send the group
    /// information. Implicit role assignments are not supported
    ///  for External connections if the underlying SSO connection is an OIDC connection.
    pub external_group_implicit_role_assignments:
        std::option::Option<std::vec::Vec<GroupImplicitRoleAssignment>>,
}
/// UpdateConnectionResponse: Response type for `External.update_connection`.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateConnectionResponse {
    /// 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,
    /// connection: The `External Connection` object affected by this API call. See the
    /// [External Connection Object](https://stytch.com/docs/b2b/api/external-connection-object) for complete
    /// response field details.
    pub connection: std::option::Option<Connection>,
}

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

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

    pub async fn create_connection(
        &self,
        body: CreateConnectionRequest,
    ) -> crate::Result<CreateConnectionResponse> {
        let organization_id = percent_encoding::utf8_percent_encode(
            &body.organization_id,
            percent_encoding::NON_ALPHANUMERIC,
        )
        .to_string();
        let path = format!("/v1/b2b/sso/external/{organization_id}");
        self.http_client
            .send(crate::Request {
                method: http::Method::POST,
                path,
                body,
            })
            .await
    }
    pub async fn update_connection(
        &self,
        body: UpdateConnectionRequest,
    ) -> crate::Result<UpdateConnectionResponse> {
        let organization_id = percent_encoding::utf8_percent_encode(
            &body.organization_id,
            percent_encoding::NON_ALPHANUMERIC,
        )
        .to_string();
        let connection_id = percent_encoding::utf8_percent_encode(
            &body.connection_id,
            percent_encoding::NON_ALPHANUMERIC,
        )
        .to_string();
        let path = format!("/v1/b2b/sso/external/{organization_id}/connections/{connection_id}");
        self.http_client
            .send(crate::Request {
                method: http::Method::PUT,
                path,
                body,
            })
            .await
    }
}
