// @generated DO NOT EDIT.
//
// Generated from the CNB OpenAPI specification by `cnb-codegen`.
// Run `cargo run -p cnb-codegen --manifest-path codegen/Cargo.toml \
//      -- --spec codegen/spec/swagger.json --out src` to refresh.

#![allow(clippy::all)]
#![allow(missing_docs)]
#![allow(unused_imports)]
#![allow(unused_mut)]

use reqwest::Method;
use serde::Serialize;

use crate::error::{ApiError, Result};
use crate::http::HttpInner;
use crate::models;

// `Organizations` resource client (generated, 12 operations).

/// Resource client.
#[derive(Debug, Clone)]
pub struct OrganizationsClient {
    inner: HttpInner,
}

impl OrganizationsClient {
    /// Construct a new resource client. Normally obtained
    /// via [`crate::ApiClient`] rather than directly.
    pub fn new(inner: HttpInner) -> Self {
        Self { inner }
    }

    /// 创建新组织。Create new organization.
    ///
    /// `POST /groups`
    pub async fn create_organization(
        &self,
        body: &crate::models::CreateGroupReq,
    ) -> Result<serde_json::Value> {
        let path = "/groups".to_string();
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }

    /// 删除指定组织。Delete the specified organization.
    ///
    /// `DELETE /{group}`
    pub async fn delete_organization(&self, group: String) -> Result<serde_json::Value> {
        let path = format!("/{}", group);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 获取指定组织信息。Get information for the specified organization.
    ///
    /// `GET /{group}`
    pub async fn get_group(&self, group: String) -> Result<crate::models::OrganizationAccess> {
        let path = format!("/{}", group);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::OrganizationAccess>(Method::GET, url)
            .await
    }

    /// 获取指定组织的配置详情。Get the configuration details for the specified organization.
    ///
    /// `GET /{slug}/-/settings`
    pub async fn get_group_setting(
        &self,
        slug: String,
    ) -> Result<crate::models::OrganizationSettingWithParent> {
        let path = format!("/{}/-/settings", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::OrganizationSettingWithParent>(Method::GET, url)
            .await
    }

    /// 获取指定用户拥有权限的顶层组织列表。 Get a list of top-level organizations that the specified user has permissions to access.
    ///
    /// `GET /users/{username}/groups`
    pub async fn get_groups_by_user_id(
        &self,
        username: String,
        query: &GetGroupsByUserIDQuery,
    ) -> Result<crate::models::OrganizationUnion> {
        let path = format!("/users/{}/groups", username);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.search {
                pairs.append_pair("search", v);
            }
            if let Some(v) = query.page {
                pairs.append_pair("page", &v.to_string());
            }
            if let Some(v) = query.page_size {
                pairs.append_pair("page_size", &v.to_string());
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::OrganizationUnion>(Method::GET, url)
            .await
    }

    /// 查询当前用户在指定组织下拥有指定权限的子组织列表。Get the list of sub-organizations that the current user has access to in the specified organization.
    ///
    /// `GET /user/groups/{slug}`
    pub async fn list_groups(
        &self,
        slug: String,
        query: &ListGroupsQuery,
    ) -> Result<Vec<crate::models::OrganizationAccess>> {
        let path = format!("/user/groups/{}", slug);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(v) = query.page {
                pairs.append_pair("page", &v.to_string());
            }
            if let Some(v) = query.page_size {
                pairs.append_pair("page_size", &v.to_string());
            }
            if let Some(v) = query.access {
                pairs.append_pair("access", &v.to_string());
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::OrganizationAccess>>(Method::GET, url)
            .await
    }

    /// 获取指定组织下的子组织列表。Get the list of sub-organizations under the specified organization.
    ///
    /// `GET /{slug}/-/sub-groups`
    pub async fn list_subgroups(
        &self,
        slug: String,
        query: &ListSubgroupsQuery,
    ) -> Result<Vec<crate::models::OrganizationUnion>> {
        let path = format!("/{}/-/sub-groups", slug);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.search {
                pairs.append_pair("search", v);
            }
            if let Some(v) = query.page {
                pairs.append_pair("page", &v.to_string());
            }
            if let Some(v) = query.page_size {
                pairs.append_pair("page_size", &v.to_string());
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::OrganizationUnion>>(Method::GET, url)
            .await
    }

    /// 获取当前用户拥有权限的顶层组织列表。Get top-level organizations list that the current user has access to.
    ///
    /// `GET /user/groups`
    pub async fn list_top_groups(
        &self,
        query: &ListTopGroupsQuery,
    ) -> Result<Vec<crate::models::OrganizationAccess>> {
        let path = "/user/groups".to_string();
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(v) = query.page {
                pairs.append_pair("page", &v.to_string());
            }
            if let Some(v) = query.page_size {
                pairs.append_pair("page_size", &v.to_string());
            }
            if let Some(ref v) = query.search {
                pairs.append_pair("search", v);
            }
            if let Some(ref v) = query.role {
                pairs.append_pair("role", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::OrganizationAccess>>(Method::GET, url)
            .await
    }

    /// 转移组织。Transfer an organization.
    ///
    /// `POST /{group}/-/transfer`
    pub async fn transfer_group(
        &self,
        group: String,
        body: &crate::models::TransferSlugReq,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/transfer", group);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }

    /// 更新指定组织的配置。Updates the configuration for the specified organization.
    ///
    /// `PUT /{slug}/-/settings`
    pub async fn update_group_setting(
        &self,
        slug: String,
        body: &crate::models::GroupSettingReq,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/settings", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::PUT, url, body)
            .await
    }

    /// 更新组织信息, 可更新的内容为: 组织描述, 组织展示名称, 组织网站, 组织联系邮箱。Updates organization information including: description, display name, website URL and contact email.
    ///
    /// `PUT /{group}`
    pub async fn update_organization(
        &self,
        group: String,
        body: &crate::models::UpdateGroupReq,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}", group);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::PUT, url, body)
            .await
    }

    /// 发起一个上传 logo 的请求，返回上传文件的url，请使用 put 发起流式上传。Initiate a request to upload logo,returns upload URL.Use PUT to initiate a stream upload.
    ///
    /// `POST /{group}/-/upload/logos`
    pub async fn upload_logos(
        &self,
        group: String,
        body: &crate::models::UploadRequestParams,
    ) -> Result<crate::models::UploadAssetsResponse> {
        let path = format!("/{}/-/upload/logos", group);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::UploadAssetsResponse, _>(Method::POST, url, body)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetGroupsByUserIDQuery {
    /// Filter organizations.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
    /// Pagination page number
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// Pagination page size
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
}

impl GetGroupsByUserIDQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
    /// Set `page` query parameter.
    pub fn page(mut self, v: impl Into<i64>) -> Self {
        self.page = Some(v.into());
        self
    }
    /// Set `page_size` query parameter.
    pub fn page_size(mut self, v: impl Into<i64>) -> Self {
        self.page_size = Some(v.into());
        self
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListGroupsQuery {
    /// Pagination page number
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// Pagination page size
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// access level
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub access: Option<i64>,
}

impl ListGroupsQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `page` query parameter.
    pub fn page(mut self, v: impl Into<i64>) -> Self {
        self.page = Some(v.into());
        self
    }
    /// Set `page_size` query parameter.
    pub fn page_size(mut self, v: impl Into<i64>) -> Self {
        self.page_size = Some(v.into());
        self
    }
    /// Set `access` query parameter.
    pub fn access(mut self, v: impl Into<i64>) -> Self {
        self.access = Some(v.into());
        self
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListSubgroupsQuery {
    /// Filter organization
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
    /// Pagination page number
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// Pagination page size
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
}

impl ListSubgroupsQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
    /// Set `page` query parameter.
    pub fn page(mut self, v: impl Into<i64>) -> Self {
        self.page = Some(v.into());
        self
    }
    /// Set `page_size` query parameter.
    pub fn page_size(mut self, v: impl Into<i64>) -> Self {
        self.page_size = Some(v.into());
        self
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListTopGroupsQuery {
    /// Pagination page number
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// Pagination page size
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// Filter by organizations.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
    /// Filter by role.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
}

impl ListTopGroupsQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `page` query parameter.
    pub fn page(mut self, v: impl Into<i64>) -> Self {
        self.page = Some(v.into());
        self
    }
    /// Set `page_size` query parameter.
    pub fn page_size(mut self, v: impl Into<i64>) -> Self {
        self.page_size = Some(v.into());
        self
    }
    /// Set `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
    /// Set `role` query parameter.
    pub fn role(mut self, v: impl Into<String>) -> Self {
        self.role = Some(v.into());
        self
    }
}
