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

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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    /// Required.
    pub workflow_id: String,
    /// Filter by source block ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_block: Option<String>,
    /// Filter by target block ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target_block: Option<String>,
    /// Edge id cursor: return the page before this id (mutually exclusive with `after`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Edge 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 edges 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(),
            source_block: Default::default(),
            target_block: Default::default(),
            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: WorkflowEdgeCreateRequest,
}

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

#[derive(Debug, Clone, Serialize)]
pub struct ListVersionsParams {
    /// Required.
    pub workflow_id: String,
    /// Filter by stable edge ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub edge_id: Option<String>,
    /// Filter by workflow version ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_version_id: Option<String>,
    /// Edge version cursor before
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Edge version cursor after
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Maximum number of edge versions to return
    ///
    /// Defaults to `50`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
}

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

#[derive(Debug, Clone, Serialize)]
pub struct ListDiffParams {
    /// Required.
    pub from_edge_version_id: String,
    /// Required.
    pub to_edge_version_id: String,
}

impl ListDiffParams {
    /// Construct a new `ListDiffParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(
        from_edge_version_id: impl Into<String>,
        to_edge_version_id: impl Into<String>,
    ) -> Self {
        Self {
            from_edge_version_id: from_edge_version_id.into(),
            to_edge_version_id: to_edge_version_id.into(),
        }
    }
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct GetParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_id: Option<String>,
}

impl<'a> WorkflowEdgesApi<'a> {
    /// List Edges
    ///
    /// List edges for a workflow with keyset cursor pagination.
    ///
    /// Optionally filter by source or target block ID. Sorted by `updated_at`
    /// descending with `id` as the tiebreaker. Pass `after` for the next
    /// page, `before` for the previous page — mutually exclusive.
    pub async fn list(&self, params: ListParams) -> Result<WorkflowEdgeList, 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<WorkflowEdgeList, Error> {
        let path = "/v1/workflows/edges".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Edge
    ///
    /// Create a new edge connecting two blocks.
    ///
    /// Validates that:
    /// - Both source and target blocks exist in the workflow
    /// - The connection is semantically valid (type compatibility, container rules, etc.)
    pub async fn create(&self, params: CreateParams) -> Result<WorkflowEdgeDoc, 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<WorkflowEdgeDoc, Error> {
        let path = "/v1/workflows/edges".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// List Edge Versions
    pub async fn list_versions(
        &self,
        params: ListVersionsParams,
    ) -> Result<WorkflowEdgeVersionList, Error> {
        self.list_versions_with_options(params, None).await
    }

    /// Variant of [`Self::list_versions`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_versions_with_options(
        &self,
        params: ListVersionsParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowEdgeVersionList, Error> {
        let path = "/v1/workflows/edges/versions".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Diff Edge Versions
    pub async fn list_diff(
        &self,
        params: ListDiffParams,
    ) -> Result<WorkflowEdgeVersionDiff, Error> {
        self.list_diff_with_options(params, None).await
    }

    /// Variant of [`Self::list_diff`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_diff_with_options(
        &self,
        params: ListDiffParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowEdgeVersionDiff, Error> {
        let path = "/v1/workflows/edges/versions/diff".to_string();
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Get Edge Version
    pub async fn get_version(&self, edge_version_id: &str) -> Result<WorkflowEdgeVersion, Error> {
        self.get_version_with_options(edge_version_id, None).await
    }

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

    /// Restore Edge Version
    pub async fn create_version_restore(
        &self,
        edge_version_id: &str,
    ) -> Result<WorkflowEdgeDoc, Error> {
        self.create_version_restore_with_options(edge_version_id, None)
            .await
    }

    /// Variant of [`Self::create_version_restore`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_version_restore_with_options(
        &self,
        edge_version_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowEdgeDoc, Error> {
        let edge_version_id = crate::client::path_segment(edge_version_id);
        let path = format!("/v1/workflows/edges/versions/{edge_version_id}/restore");
        let method = http::Method::POST;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Get Edge
    ///
    /// Get a single edge by ID.
    pub async fn get(&self, edge_id: &str, params: GetParams) -> Result<WorkflowEdgeDoc, Error> {
        self.get_with_options(edge_id, params, None).await
    }

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

    /// Delete Edge
    ///
    /// Delete an edge from a workflow.
    pub async fn delete(&self, edge_id: &str) -> Result<(), Error> {
        self.delete_with_options(edge_id, None).await
    }

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