// 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::*;
use crate::resources::experiment_run_metrics::ExperimentRunMetricsApi;
use crate::resources::experiment_run_results::ExperimentRunResultsApi;
use crate::resources::experiment_runs::ExperimentRunsApi;
#[allow(unused_imports)]
use serde::Serialize;

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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    /// Required.
    pub workflow_id: 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 `50`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Defaults to `desc`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<WorkflowExperimentsOrder>,
}

impl ListParams {
    /// Construct a new `ListParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(workflow_id: impl Into<String>) -> Self {
        Self {
            workflow_id: workflow_id.into(),
            before: Default::default(),
            after: Default::default(),
            limit: Some(50),
            order: Some(WorkflowExperimentsOrder::Desc),
        }
    }
}

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

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

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

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

impl<'a> WorkflowExperimentsApi<'a> {
    /// Access the `metrics` sub-resource.
    pub fn metrics(&self) -> ExperimentRunMetricsApi<'a> {
        ExperimentRunMetricsApi {
            client: self.client,
        }
    }

    /// Access the `results` sub-resource.
    pub fn results(&self) -> ExperimentRunResultsApi<'a> {
        ExperimentRunResultsApi {
            client: self.client,
        }
    }

    /// Access the `runs` sub-resource.
    pub fn runs(&self) -> ExperimentRunsApi<'a> {
        ExperimentRunsApi {
            client: self.client,
        }
    }

    /// List Experiments
    ///
    /// List experiments under one workflow with cursor pagination.
    ///
    /// Each experiment is returned with its latest-run snapshot, block info, and
    /// drift detection.
    pub async fn list(&self, params: ListParams) -> Result<WorkflowExperimentList, 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<WorkflowExperimentList, Error> {
        let path = "/v1/workflows/experiments".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Experiment
    ///
    /// Create an experiment.
    ///
    /// When `source_experiment_id` is set, duplicates the source experiment
    /// (block, name + "(Copy)", n_consensus, documents) and rejects any other
    /// field. Otherwise creates a fresh experiment from the provided fields.
    pub async fn create(&self, params: CreateParams) -> Result<WorkflowExperiment, 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<WorkflowExperiment, Error> {
        let path = "/v1/workflows/experiments".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Experiment
    ///
    /// Retrieve a single experiment.
    ///
    /// Identified by `experiment_id`. Returns the experiment along with its
    /// latest-run status, score, staleness, and schema-drift detection. Returns
    /// 404 if no experiment with that ID exists.
    pub async fn get(&self, experiment_id: &str) -> Result<WorkflowExperiment, Error> {
        self.get_with_options(experiment_id, None).await
    }

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

    /// Update Experiment
    ///
    /// Update an experiment.
    ///
    /// Identified by `experiment_id`. Send any of `name`, `n_consensus`,
    /// `documents`, or `document_captures`; omitted fields are left unchanged.
    /// Returns the updated experiment with its latest-run status and drift info.
    pub async fn update(
        &self,
        experiment_id: &str,
        params: UpdateParams,
    ) -> Result<WorkflowExperiment, Error> {
        self.update_with_options(experiment_id, params, None).await
    }

    /// Variant of [`Self::update`] that accepts per-request [`crate::RequestOptions`].
    pub async fn update_with_options(
        &self,
        experiment_id: &str,
        params: UpdateParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowExperiment, Error> {
        let experiment_id = crate::client::path_segment(experiment_id);
        let path = format!("/v1/workflows/experiments/{experiment_id}");
        let method = http::Method::PATCH;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Delete Experiment
    ///
    /// Delete an experiment.
    ///
    /// Identified by `experiment_id`. Also removes the experiment's runs and
    /// results. Returns 204 on success, 404 if not found, and 409 if the latest
    /// run is still pending or running (cancel it first).
    pub async fn delete(&self, experiment_id: &str) -> Result<(), Error> {
        self.delete_with_options(experiment_id, None).await
    }

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