// @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;

// `Missions` resource client (generated, 9 operations).

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

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

    /// 创建任务集。Create a mission.
    ///
    /// `POST /{slug}/-/missions`
    pub async fn create_mission(
        &self,
        slug: String,
        body: &crate::models::CreateMissionReq,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/missions", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }

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

    /// 查询组织下面用户有权限查看到的任务集。Query all missions that the user has permission to see under the specific organization.
    ///
    /// `GET /{slug}/-/missions`
    pub async fn get_group_sub_missions(
        &self,
        slug: String,
        query: &GetGroupSubMissionsQuery,
    ) -> Result<Vec<crate::models::Missions4User>> {
        let path = format!("/{}/-/missions", 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(ref v) = query.filter_type {
                pairs.append_pair("filter_type", v);
            }
            if let Some(ref v) = query.order_by {
                pairs.append_pair("order_by", v);
            }
            if let Some(v) = query.desc {
                pairs.append_pair("desc", &v.to_string());
            }
            if let Some(ref v) = query.descendant {
                pairs.append_pair("descendant", v);
            }
            if let Some(ref v) = query.search {
                pairs.append_pair("search", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::Missions4User>>(Method::GET, url)
            .await
    }

    /// 查询任务集视图配置信息。Get mission view config.
    ///
    /// `GET /{mission}/-/mission/view`
    pub async fn get_mission_view_config(
        &self,
        mission: String,
        query: &GetMissionViewConfigQuery,
    ) -> Result<crate::models::MissionViewConfig> {
        let path = format!("/{}/-/mission/view", mission);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.id {
                pairs.append_pair("id", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::MissionViewConfig>(Method::GET, url)
            .await
    }

    /// 获取任务集视图列表。Get view list of a mission.
    ///
    /// `GET /{mission}/-/mission/view-list`
    pub async fn get_mission_view_list(
        &self,
        mission: String,
    ) -> Result<Vec<crate::models::MissionView>> {
        let path = format!("/{}/-/mission/view-list", mission);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<Vec<crate::models::MissionView>>(Method::GET, url)
            .await
    }

    /// 设置任务集视图配置信息。Set mission view config.
    ///
    /// `POST /{mission}/-/mission/view`
    pub async fn post_mission_view_config(
        &self,
        mission: String,
        body: &crate::models::MissionViewConfig,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/mission/view", mission);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }

    /// 排序任务集视图。Sort mission view list.
    ///
    /// `POST /{mission}/-/mission/view-list`
    pub async fn post_mission_view_list(
        &self,
        mission: String,
        body: &crate::models::MissionPostViewReq,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/mission/view-list", mission);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }

    /// 添加、修改任务集视图。Update a mission view or add a new one.
    ///
    /// `PUT /{mission}/-/mission/view-list`
    pub async fn put_mission_view_list(
        &self,
        mission: String,
        body: &crate::models::MissionView,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/mission/view-list", mission);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::PUT, url, body)
            .await
    }

    /// 改变任务集可见性。Update the visibility of a mission.
    ///
    /// `POST /{mission}/-/settings/set_visibility`
    pub async fn set_mission_visibility(
        &self,
        mission: String,
        query: &SetMissionVisibilityQuery,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/settings/set_visibility", mission);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.visibility {
                pairs.append_pair("visibility", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<serde_json::Value>(Method::POST, url)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetGroupSubMissionsQuery {
    /// 页码
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// 每页数量
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// 任务集类型
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub filter_type: Option<String>,
    /// 排序类型，默认created_at
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub order_by: Option<String>,
    /// 排序顺序
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<bool>,
    /// 查全部/查询直接属于当前组织的仓库/查询子组织的仓库
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub descendant: Option<String>,
    /// 搜索关键字
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
}

impl GetGroupSubMissionsQuery {
    /// 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 `filter_type` query parameter.
    pub fn filter_type(mut self, v: impl Into<String>) -> Self {
        self.filter_type = Some(v.into());
        self
    }
    /// Set `order_by` query parameter.
    pub fn order_by(mut self, v: impl Into<String>) -> Self {
        self.order_by = Some(v.into());
        self
    }
    /// Set `desc` query parameter.
    pub fn desc(mut self, v: impl Into<bool>) -> Self {
        self.desc = Some(v.into());
        self
    }
    /// Set `descendant` query parameter.
    pub fn descendant(mut self, v: impl Into<String>) -> Self {
        self.descendant = Some(v.into());
        self
    }
    /// Set `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetMissionViewConfigQuery {
    /// View ID
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
}

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

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct SetMissionVisibilityQuery {
    /// 任务集可见性
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub visibility: Option<String>,
}

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