// 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 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> {
    /// List Experiments
    ///
    /// List experiments under one workflow with cursor pagination.
    ///
    /// The enrichment passes (latest-run snapshot, block info, drift detection)
    /// run on the paginated page, not the full collection — so they scale with
    /// ``limit``, not with the total experiment count under the workflow.
    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_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Returns an async [`futures_util::Stream`] that yields every `WorkflowExperiment`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<WorkflowExperiment> = self
    ///     .list_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_auto_paging(
        &self,
        params: ListParams,
    ) -> impl futures_util::Stream<Item = Result<WorkflowExperiment, Error>> + '_ {
        crate::pagination::auto_paginate_pages(move |after| {
            let mut params = params.clone();
            params.after = after;
            async move {
                let page = self.list(params).await?;
                Ok((page.data, page.list_metadata.after))
            }
        })
    }

    /// 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
    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
    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
    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
    }
}
