// Code generated by oagen. DO NOT EDIT.

use crate::client::Retab;
#[allow(unused_imports)]
use crate::enums::*;
use crate::error::Error;
#[allow(unused_imports)]
use crate::models::*;
#[allow(unused_imports)]
use serde::Serialize;

pub struct ExperimentRunsApi<'a> {
    pub(crate) client: &'a Retab,
}

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub experiment_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<WorkflowExperimentsStatus>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclude_status: Option<WorkflowExperimentsExcludeStatus>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_date: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_date: Option<String>,
    /// Defaults to `created_at`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort_by: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Defaults to `20`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Defaults to `desc`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<ExperimentRunsOrder>,
}

impl Default for ListParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            workflow_id: Default::default(),
            experiment_id: Default::default(),
            block_id: Default::default(),
            status: Default::default(),
            exclude_status: Default::default(),
            trigger_type: Default::default(),
            from_date: Default::default(),
            to_date: Default::default(),
            sort_by: Some("created_at".to_string()),
            before: Default::default(),
            after: Default::default(),
            limit: Some(20),
            order: Some(ExperimentRunsOrder::Desc),
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: CreateExperimentRunRequest,
}

impl CreateParams {
    /// Construct a new `CreateParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: CreateExperimentRunRequest) -> Self {
        Self { body }
    }
}

impl<'a> ExperimentRunsApi<'a> {
    /// List Experiment Runs
    ///
    /// List experiment runs.
    ///
    /// Optionally filter by `workflow_id`, `experiment_id`, `block_id`,
    /// `status`/`exclude_status`, `trigger_type`, and a `from_date`/`to_date`
    /// window. Returns a cursor-paginated list ordered by `sort_by` (default
    /// newest first).
    pub async fn list(&self, params: ListParams) -> Result<WorkflowExperimentRunList, Error> {
        self.list_with_options(params, None).await
    }

    /// Variant of [`Self::list`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_with_options(
        &self,
        params: ListParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowExperimentRunList, Error> {
        let path = "/v1/workflows/experiments/runs".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Experiment Run Flat
    ///
    /// Create an experiment run.
    ///
    /// The `experiment_id` and an optional `workflow_id` are supplied in the body.
    /// When `workflow_id` is omitted, the experiment's workflow is used; when
    /// supplied, it must match that workflow or the request is rejected with 404.
    pub async fn create(&self, params: CreateParams) -> Result<ExperimentRun, Error> {
        self.create_with_options(params, None).await
    }

    /// Variant of [`Self::create`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_with_options(
        &self,
        params: CreateParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<ExperimentRun, Error> {
        let path = "/v1/workflows/experiments/runs".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Experiment Run
    ///
    /// Retrieve a single experiment run.
    ///
    /// Identified by `run_id`. Returns the run with its lifecycle status, timing,
    /// score, and document progress counts. Returns 404 if no run with that ID
    /// exists.
    pub async fn get(&self, run_id: &str) -> Result<ExperimentRun, Error> {
        self.get_with_options(run_id, None).await
    }

    /// Variant of [`Self::get`] that accepts per-request [`crate::RequestOptions`].
    pub async fn get_with_options(
        &self,
        run_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<ExperimentRun, Error> {
        let run_id = crate::client::path_segment(run_id);
        let path = format!("/v1/workflows/experiments/runs/{run_id}");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Cancel Experiment Run
    ///
    /// Cancel an experiment run.
    ///
    /// Identified by `run_id`. Cancels the run and any of its pending or
    /// in-flight results, returning the run's new `cancelled` lifecycle. Returns
    /// 404 if the run does not exist or is not in a cancellable (pending, queued,
    /// or running) state.
    pub async fn cancel(&self, run_id: &str) -> Result<CancelWorkflowExperimentRunResponse, Error> {
        self.cancel_with_options(run_id, None).await
    }

    /// Variant of [`Self::cancel`] that accepts per-request [`crate::RequestOptions`].
    pub async fn cancel_with_options(
        &self,
        run_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<CancelWorkflowExperimentRunResponse, Error> {
        let run_id = crate::client::path_segment(run_id);
        let path = format!("/v1/workflows/experiments/runs/{run_id}/cancel");
        let method = http::Method::POST;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }
}
