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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    /// Required.
    pub run_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<WorkflowTestRunResultsOrder>,
}

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

impl<'a> WorkflowTestRunResultsApi<'a> {
    /// List Test Execution Results
    ///
    /// List workflow test results for a single run, page by page.
    ///
    /// Results are returned in run-time order.
    pub async fn list(&self, params: ListParams) -> Result<WorkflowTestResultList, 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<WorkflowTestResultList, Error> {
        let path = "/v1/workflows/tests/results".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Get Test Execution Result
    ///
    /// Retrieve a single workflow test result.
    ///
    /// Identified by `result_id`. Returns the result for one test within a run,
    /// including its `verdict` (`passed`, `failed`, or `blocked`), lifecycle,
    /// timing, and any error. Returns 404 if no result with that ID exists.
    pub async fn get(&self, result_id: &str) -> Result<WorkflowTestResult, Error> {
        self.get_with_options(result_id, None).await
    }

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