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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    /// Required.
    pub run_id: String,
    /// Required.
    pub block_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 `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<WorkflowBlockExecutionsOrder>,
}

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

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

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

impl<'a> WorkflowBlockExecutionsApi<'a> {
    /// List Block Executions
    ///
    /// List recent block executions for one workflow run block.
    ///
    /// Cursor pagination matches the conventions used by
    /// ``GET /v1/extractions`` — pass ``after`` from the previous page's
    /// ``list_metadata.after`` to advance, ``before`` to step backwards, and
    /// ``order`` to flip the sort direction. ``run_id`` + ``block_id`` are
    /// required scope filters; without them this endpoint would expose
    /// cross-run cursors that walk arbitrary block executions.
    pub async fn list(&self, params: ListParams) -> Result<BlockExecutionList, 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<BlockExecutionList, Error> {
        let path = "/v1/workflows/blocks/executions".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 `StoredBlockExecution`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<StoredBlockExecution> = self
    ///     .list_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_auto_paging(
        &self,
        params: ListParams,
    ) -> impl futures_util::Stream<Item = Result<StoredBlockExecution, 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 Block Execution
    ///
    /// Create a block execution for ``block_id`` against the current draft.
    pub async fn create(&self, params: CreateParams) -> Result<StoredBlockExecution, 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<StoredBlockExecution, Error> {
        let path = "/v1/workflows/blocks/executions".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }
}
