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

#[derive(Debug, Clone, Serialize)]
pub struct ListParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Defaults to `10`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Defaults to `desc`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<SplitsOrder>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<SplitsStatus>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_date: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_date: Option<String>,
}

impl Default for ListParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            before: Default::default(),
            after: Default::default(),
            limit: Some(10),
            order: Some(SplitsOrder::Desc),
            filename: Default::default(),
            status: Default::default(),
            from_date: Default::default(),
            to_date: Default::default(),
        }
    }
}

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

impl CreateParams {
    /// Construct a new `CreateParams` with the required fields set.
    ///
    /// Accepts ergonomic input for MimeData-typed fields via
    /// [`Into<MimeData>`](crate::MimeData) — pass a `PathBuf`,
    /// `Vec<u8>`, `&str` URL, or pre-built `MimeData`.
    #[allow(deprecated)]
    pub fn new<D: Into<crate::MimeData>>(document: D, subdocuments: Vec<Subdocument>) -> Self {
        Self {
            body: SplitRequest {
                document: document.into(),
                subdocuments,
                model: Default::default(),
                instructions: Default::default(),
                n_consensus: Default::default(),
                bust_cache: Default::default(),
                background: Default::default(),
            },
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct GetParams {
    /// When false, returns a cheap status-only projection (no output), served from cache for in-flight background runs.
    ///
    /// Defaults to `true`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_output: Option<bool>,
}

impl Default for GetParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            include_output: Some(true),
        }
    }
}

impl<'a> SplitsApi<'a> {
    /// List Splits
    ///
    /// List splits.
    ///
    /// Returns a paginated list of splits for the authenticated environment, newest first by
    /// default. Filter by `filename` prefix (case-insensitive) and by a `created_at` window
    /// using `from_date`/`to_date` (`YYYY-MM-DD`). Page through results with `before`/`after`,
    /// `limit`, and `order`.
    pub async fn list(&self, params: ListParams) -> Result<SplitList, 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<SplitList, Error> {
        let path = "/v1/splits".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Split
    ///
    /// Create a split.
    ///
    /// Divides a `document` into the named `subdocuments`, assigning each its set of pages,
    /// using the chosen `model` and optional `instructions`. Set `n_consensus` above `1` to
    /// run multiple votes and consolidate them. Returns the stored `Split` with its `output`
    /// page assignments, and responds with `201`.
    pub async fn create(&self, params: CreateParams) -> Result<Split, 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<Split, Error> {
        let path = "/v1/splits".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Split
    ///
    /// Retrieve a split.
    ///
    /// Fetches a single split by its `split_id` within the authenticated environment and
    /// returns the full `Split` including its `output` page assignments. Responds with `404`
    /// if no split with that id exists.
    pub async fn get(&self, split_id: &str, params: GetParams) -> Result<Split, Error> {
        self.get_with_options(split_id, params, None).await
    }

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

    /// Delete Split
    ///
    /// Delete a split.
    ///
    /// Permanently deletes the split identified by `split_id`. Returns `204` on
    /// success, or `404` if no split with that id exists.
    pub async fn delete(&self, split_id: &str) -> Result<(), Error> {
        self.delete_with_options(split_id, None).await
    }

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

    /// Cancel Split
    pub async fn create_split_cancel(&self, split_id: &str) -> Result<Split, Error> {
        self.create_split_cancel_with_options(split_id, None).await
    }

    /// Variant of [`Self::create_split_cancel`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_split_cancel_with_options(
        &self,
        split_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<Split, Error> {
        let split_id = crate::client::path_segment(split_id);
        let path = format!("/v1/splits/{split_id}/cancel");
        let method = http::Method::POST;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }
}
