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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    /// Workflow run ID whose artifacts should be listed. Required unless ``step_id`` is provided.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
    /// Optional artifact operation filter
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operation: Option<WorkflowArtifactsOperation>,
    /// Optional block_id or step_id filter
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
    /// Optional step id filter. When provided, returns the single artifact attached to that step (or an empty list if the step has no artifact). ``run_id`` is not required when ``step_id`` is set — it is resolved from the step record.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_id: Option<String>,
    /// Step id cursor: return the page before this step (mutually exclusive with ``after``). Ignored when ``step_id`` is set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Step id cursor: return the page after this step (mutually exclusive with ``before``). Ignored when ``step_id`` is set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Maximum number of artifacts to return per page (1-200). Ignored when ``step_id`` is set (that path returns the single attached artifact).
    ///
    /// Defaults to `100`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
}

impl Default for ListParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            run_id: Default::default(),
            operation: Default::default(),
            block_id: Default::default(),
            step_id: Default::default(),
            before: Default::default(),
            after: Default::default(),
            limit: Some(100),
        }
    }
}

impl<'a> WorkflowArtifactsApi<'a> {
    /// Get Workflow Artifact By Id
    ///
    /// Get one workflow artifact by id alone.
    ///
    /// The operation is derived from the id prefix
    /// (``extr_…`` → extraction, ``clss_…`` → classification, etc.). This is
    /// the flat-resource shape — callers do not need to know which collection
    /// backs the id.
    pub async fn get(&self, artifact_id: &str) -> Result<serde_json::Value, Error> {
        self.get_with_options(artifact_id, None).await
    }

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

    /// List Workflow Artifacts
    ///
    /// List artifacts produced by a workflow run.
    ///
    /// Paginated by the producing step's ``step_id`` (sorted by ``started_at``
    /// ascending). Pass ``after`` for the next page, ``before`` for the previous
    /// page — mutually exclusive. ``step_id`` short-circuits pagination and
    /// returns the single attached artifact.
    ///
    /// Filters: provide either ``run_id`` (list all artifacts in a run) or
    /// ``step_id`` (single-step lookup). When both are absent the request is
    /// rejected with 400.
    pub async fn list(&self, params: ListParams) -> Result<WorkflowArtifactList, 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<WorkflowArtifactList, Error> {
        let path = "/v1/workflows/artifacts".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 `WorkflowArtifact`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<WorkflowArtifact> = self
    ///     .list_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_auto_paging(
        &self,
        params: ListParams,
    ) -> impl futures_util::Stream<Item = Result<WorkflowArtifact, 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))
            }
        })
    }
}
