// 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 WorkflowsApi<'a> {
    pub(crate) client: &'a Retab,
}

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Items per page
    ///
    /// Defaults to `10`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Defaults to `desc`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<WorkflowsOrder>,
    /// Defaults to `updated_at`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort_by: Option<String>,
}

impl Default for ListParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            before: Default::default(),
            after: Default::default(),
            limit: Some(10),
            order: Some(WorkflowsOrder::Desc),
            sort_by: Some("updated_at".to_string()),
        }
    }
}

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

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

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

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

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

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

#[derive(Debug, Clone, Default, Serialize)]
pub struct PublishParams {
    /// Request body sent with this call.
    #[serde(skip)]
    pub body: Option<PublishWorkflowRequest>,
}

impl<'a> WorkflowsApi<'a> {
    /// List Workflows
    ///
    /// List workflows with pagination and optional filtering.
    pub async fn list(&self, params: ListParams) -> Result<WorkflowList, 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<WorkflowList, Error> {
        let path = "/v1/workflows".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 `Workflow`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<Workflow> = self
    ///     .list_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_auto_paging(
        &self,
        params: ListParams,
    ) -> impl futures_util::Stream<Item = Result<Workflow, 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 Workflow
    ///
    /// Create a new workflow.
    ///
    /// The workflow starts unpublished and is scaffolded with a default
    /// "Document" input block in the live block collection.
    pub async fn create(&self, params: CreateParams) -> Result<Workflow, 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<Workflow, Error> {
        let path = "/v1/workflows".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Workflow
    ///
    /// Get a single workflow by ID.
    ///
    /// Returns workflow metadata only.
    pub async fn get(&self, workflow_id: &str) -> Result<Workflow, Error> {
        self.get_with_options(workflow_id, None).await
    }

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

    /// Update Workflow
    ///
    /// Update an existing workflow.
    pub async fn update(&self, workflow_id: &str, params: UpdateParams) -> Result<Workflow, Error> {
        self.update_with_options(workflow_id, params, None).await
    }

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

    /// Delete Workflow
    ///
    /// Delete a workflow and all its associated entities.
    ///
    /// This deletes:
    /// - The workflow document
    /// - All blocks and edges (live collections)
    /// - All block and edge snapshots
    /// - All workflow snapshots
    pub async fn delete(&self, workflow_id: &str) -> Result<(), Error> {
        self.delete_with_options(workflow_id, None).await
    }

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

    /// Diagnose Workflow Graph
    ///
    /// Diagnose a workflow graph payload for the given workflow.
    ///
    /// When the request omits ``blocks`` and ``edges`` (both ``None``) the route
    /// loads the persisted draft from MongoDB. When either is provided (including
    /// an explicit empty list) the request body is diagnosed as-is.
    pub async fn diagnose(
        &self,
        workflow_id: &str,
        params: DiagnoseParams,
    ) -> Result<WorkflowDiagnosisResponse, Error> {
        self.diagnose_with_options(workflow_id, params, None).await
    }

    /// Variant of [`Self::diagnose`] that accepts per-request [`crate::RequestOptions`].
    pub async fn diagnose_with_options(
        &self,
        workflow_id: &str,
        params: DiagnoseParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowDiagnosisResponse, Error> {
        let workflow_id = crate::client::path_segment(workflow_id);
        let path = format!("/v1/workflows/{workflow_id}/diagnose-graph");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Publish Workflow
    ///
    /// Publish a workflow.
    ///
    /// This creates an immutable snapshot of the workflow configuration, making it available for workflow runs.
    /// The live entities remain unchanged so users can continue editing.
    pub async fn publish(
        &self,
        workflow_id: &str,
        params: PublishParams,
    ) -> Result<Workflow, Error> {
        self.publish_with_options(workflow_id, params, None).await
    }

    /// Variant of [`Self::publish`] that accepts per-request [`crate::RequestOptions`].
    pub async fn publish_with_options(
        &self,
        workflow_id: &str,
        params: PublishParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<Workflow, Error> {
        let workflow_id = crate::client::path_segment(workflow_id);
        let path = format!("/v1/workflows/{workflow_id}/publish");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, params.body.as_ref(), options)
            .await
    }
}
