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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workflow_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub iteration_key: Option<String>,
    /// Filter by decision state: pending, approved, rejected, decided, or all.
    ///
    /// Defaults to `pending`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub decision_status: Option<ReviewDecisionStatus>,
    /// Cursor: only return reviews that appear before this review id in the result order. Use list_metadata.before from the previous page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Cursor: only return reviews that appear after this review id in the result order. Use list_metadata.after from the previous page.
    #[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>,
}

impl Default for ListParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            workflow_id: Default::default(),
            run_id: Default::default(),
            block_id: Default::default(),
            step_id: Default::default(),
            iteration_key: Default::default(),
            decision_status: Some(ReviewDecisionStatus::Pending),
            before: Default::default(),
            after: Default::default(),
            limit: Some(50),
        }
    }
}

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

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

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

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

impl<'a> WorkflowReviewsApi<'a> {
    /// List Reviews Route
    ///
    /// List reviews — the review queue, oldest first by ``created_at``.
    pub async fn list(&self, params: ListParams) -> Result<WorkflowReviewList, 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<WorkflowReviewList, Error> {
        let path = "/v1/workflows/reviews".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 `Review`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<Review> = self
    ///     .list_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_auto_paging(
        &self,
        params: ListParams,
    ) -> impl futures_util::Stream<Item = Result<Review, 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))
            }
        })
    }

    /// Get Review Route
    ///
    /// Read one review's metadata + decision. Versions are fetched separately.
    pub async fn get(&self, review_id: &str) -> Result<Review, Error> {
        self.get_with_options(review_id, None).await
    }

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

    /// Approve Review Route
    ///
    /// Approve one exact review version and resume the Temporal run.
    ///
    /// Earns its action-verb shape per the four criteria in
    /// ``meta-pattern-blueprint.md`` §2: precondition (``decision is None``),
    /// side-effect dominates (Temporal resume signal), divergent request body vs
    /// ``/reject``, divergent response (carries ``resume_status``).
    pub async fn approve(
        &self,
        review_id: &str,
        params: ApproveParams,
    ) -> Result<SubmitDecisionResponse, Error> {
        self.approve_with_options(review_id, params, None).await
    }

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

    /// Reject Review Route
    ///
    /// Reject one exact review version and resume the Temporal run.
    ///
    /// ``reason`` is required by the request shape — "rejected without reason"
    /// is unrepresentable on the wire.
    pub async fn reject(
        &self,
        review_id: &str,
        params: RejectParams,
    ) -> Result<SubmitDecisionResponse, Error> {
        self.reject_with_options(review_id, params, None).await
    }

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