// 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::workflow_block_executions::WorkflowBlockExecutionsApi;
#[allow(unused_imports)]
use serde::Serialize;

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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    /// Required.
    pub workflow_id: String,
    /// Block id cursor: return the page before this id (mutually exclusive with `after`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Block id cursor: return the page after this id (mutually exclusive with `before`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Maximum number of blocks to return per page (1-200).
    ///
    /// Defaults to `100`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
}

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(100),
        }
    }
}

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

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

#[derive(Debug, Clone, Default, Serialize)]
pub struct GetParams {
    /// Disambiguates a block id that is shared by more than one workflow. Required only when the block id is not unique within your organization — otherwise the call returns 409 listing the colliding workflow_ids. Server-generated block IDs are always unique and never need this.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_id: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdateParams {
    /// Disambiguates a block id that is shared by more than one workflow. Required only when the block id is not unique within your organization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_id: Option<String>,
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: UpdateWorkflowBlockRequest,
}

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

#[derive(Debug, Clone, Default, Serialize)]
pub struct DeleteParams {
    /// Disambiguates a block id that is shared by more than one workflow. Required only when the block id is not unique within your organization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_id: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateBlockValidateConfigParams {
    /// Workflow ID to disambiguate legacy duplicate block IDs. Omit for normal server-generated block IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_id: Option<String>,
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: ValidateWorkflowBlockConfigRequest,
}

impl CreateBlockValidateConfigParams {
    /// Construct a new `CreateBlockValidateConfigParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: ValidateWorkflowBlockConfigRequest) -> Self {
        Self {
            workflow_id: Default::default(),
            body,
        }
    }
}

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

    /// List Blocks
    ///
    /// List blocks for a workflow with keyset cursor pagination.
    ///
    /// Sorted by `updated_at` descending with `id` as the tiebreaker. Pass
    /// `after` (the previous response's `list_metadata.after`) for the next
    /// page, `before` for the previous page. They are mutually exclusive; the
    /// 400 cleanly tells the caller which to drop.
    pub async fn list(&self, params: ListParams) -> Result<WorkflowBlockList, 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<WorkflowBlockList, Error> {
        let path = "/v1/workflows/blocks".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Block
    ///
    /// Create a new block in a workflow.
    pub async fn create(&self, params: CreateParams) -> Result<WorkflowBlock, 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<WorkflowBlock, Error> {
        let path = "/v1/workflows/blocks".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Block
    ///
    /// Get a single block by ID.
    pub async fn get(&self, block_id: &str, params: GetParams) -> Result<WorkflowBlock, Error> {
        self.get_with_options(block_id, params, None).await
    }

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

    /// Update Block
    ///
    /// Update a block with partial data.
    ///
    /// Only the provided fields are updated. This enables targeted updates
    /// like position changes without affecting other block properties.
    pub async fn update(
        &self,
        block_id: &str,
        params: UpdateParams,
    ) -> Result<WorkflowBlock, Error> {
        self.update_with_options(block_id, params, None).await
    }

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

    /// Delete Block
    ///
    /// Delete a block from a workflow.
    ///
    /// This also deletes any edges connected to this block.
    pub async fn delete(&self, block_id: &str, params: DeleteParams) -> Result<(), Error> {
        self.delete_with_options(block_id, params, None).await
    }

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

    /// Validate Block Config Dry Run
    ///
    /// Validate an assembled block config without mutating the workflow draft.
    pub async fn create_block_validate_config(
        &self,
        block_id: &str,
        params: CreateBlockValidateConfigParams,
    ) -> Result<ValidateWorkflowBlockConfigResponse, Error> {
        self.create_block_validate_config_with_options(block_id, params, None)
            .await
    }

    /// Variant of [`Self::create_block_validate_config`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_block_validate_config_with_options(
        &self,
        block_id: &str,
        params: CreateBlockValidateConfigParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<ValidateWorkflowBlockConfigResponse, Error> {
        let block_id = crate::client::path_segment(block_id);
        let path = format!("/v1/workflows/blocks/{block_id}/validate-config");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }
}
