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

// `Workspace` resource client (generated, 5 operations).

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

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

    /// 删除我的云原生开发环境。Delete my workspace.
    ///
    /// `POST /workspace/delete`
    pub async fn delete_workspace(
        &self,
        body: &crate::models::WorkspaceDeleteReq,
    ) -> Result<crate::models::WorkspaceDeleteResult> {
        let path = "/workspace/delete".to_string();
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::WorkspaceDeleteResult, _>(Method::POST, url, body)
            .await
    }

    /// 根据流水线sn查询云原生开发访问地址。Query cloud-native development access address by pipeline SN.
    ///
    /// `GET /{repo}/-/workspace/detail/{sn}`
    pub async fn get_workspace_detail(
        &self,
        repo: String,
        sn: String,
    ) -> Result<crate::models::WorkspaceDetailResult> {
        let path = format!("/{}/-/workspace/detail/{}", repo, sn);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::WorkspaceDetailResult>(Method::GET, url)
            .await
    }

    /// 获取我的云原生开发环境列表。List my workspaces.
    ///
    /// `GET /workspace/list`
    pub async fn list_workspaces(
        &self,
        query: &ListWorkspacesQuery,
    ) -> Result<crate::models::WorkspaceListResult> {
        let path = "/workspace/list".to_string();
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.branch {
                pairs.append_pair("branch", v);
            }
            if let Some(ref v) = query.end {
                pairs.append_pair("end", 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());
            }
            if let Some(ref v) = query.slug {
                pairs.append_pair("slug", v);
            }
            if let Some(ref v) = query.start {
                pairs.append_pair("start", v);
            }
            if let Some(ref v) = query.status {
                pairs.append_pair("status", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::WorkspaceListResult>(Method::GET, url)
            .await
    }

    /// 启动云原生开发环境，已存在环境则直接打开，否则重新创建开发环境。Start cloud-native dev. Opens existing env or creates a new one.
    ///
    /// `POST /{repo}/-/workspace/start`
    pub async fn start_workspace(
        &self,
        repo: String,
        body: &crate::models::StartWorkspaceReq,
    ) -> Result<crate::models::StartWorkspaceResult> {
        let path = format!("/{}/-/workspace/start", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::StartWorkspaceResult, _>(Method::POST, url, body)
            .await
    }

    /// 停止/关闭我的云原生开发环境。Stop/close my workspace.
    ///
    /// `POST /workspace/stop`
    pub async fn workspace_stop(
        &self,
        body: &crate::models::WorkspaceStopReq,
    ) -> Result<crate::models::WorkspaceStopResult> {
        let path = "/workspace/stop".to_string();
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::WorkspaceStopResult, _>(Method::POST, url, body)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListWorkspacesQuery {
    /// Git branch name, e.g. "main"
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub branch: Option<String>,
    /// 查询结束时间。Query end time. format YYYY-MM-DD HH:mm:ssZZ, e.g. 2024-12-01 00:00:00+0800
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub end: Option<String>,
    /// Pagination page number, default(1)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// Pagination page size, default(20), max(100)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// Repository path, e.g. "groupname/reponame"
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub slug: Option<String>,
    /// 查询开始时间。Query start time. format YYYY-MM-DD HH:mm:ssZZ, e.g. 2024-12-01 00:00:00+0800
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub start: Option<String>,
    /// 开发环境状态，running: 开发环境已启动，closed：开发环境已关闭。Workspace status: "running" for started, "closed" for stopped.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

impl ListWorkspacesQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `branch` query parameter.
    pub fn branch(mut self, v: impl Into<String>) -> Self {
        self.branch = Some(v.into());
        self
    }
    /// Set `end` query parameter.
    pub fn end(mut self, v: impl Into<String>) -> Self {
        self.end = 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
    }
    /// Set `slug` query parameter.
    pub fn slug(mut self, v: impl Into<String>) -> Self {
        self.slug = Some(v.into());
        self
    }
    /// Set `start` query parameter.
    pub fn start(mut self, v: impl Into<String>) -> Self {
        self.start = Some(v.into());
        self
    }
    /// Set `status` query parameter.
    pub fn status(mut self, v: impl Into<String>) -> Self {
        self.status = Some(v.into());
        self
    }
}
