// 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 PartitionsApi<'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<PartitionsOrder>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<PartitionsStatus>,
    #[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(PartitionsOrder::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: PartitionRequest,
}

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,
        key: impl Into<String>,
        instructions: impl Into<String>,
    ) -> Self {
        Self {
            body: PartitionRequest {
                document: ClassificationRequestDocumentOneOf::MimeData(Box::new(document.into())),
                key: key.into(),
                instructions: instructions.into(),
                model: Default::default(),
                n_consensus: Default::default(),
                allow_overlap: 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> PartitionsApi<'a> {
    /// List Partitions
    ///
    /// List partitions.
    ///
    /// Returns a paginated list of partitions 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<PartitionList, 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<PartitionList, Error> {
        let path = "/v1/partitions".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Partitions
    ///
    /// Create a partition.
    ///
    /// Groups the pages of a `document` into chunks by a partition `key`, guided by
    /// `instructions` and the chosen `model`. Set `n_consensus` above `1` to run multiple
    /// votes and consolidate them, and `allow_overlap` to let a page belong to more than one
    /// chunk. Returns the stored `Partition` with its `output` chunks, and responds with `201`.
    pub async fn create(&self, params: CreateParams) -> Result<Partition, 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<Partition, Error> {
        let path = "/v1/partitions".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

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

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

    /// Delete Partition
    ///
    /// Delete a partition.
    ///
    /// Permanently deletes the partition identified by `partition_id` in the authenticated
    /// environment. Returns `204` with no body on success, or `404` if the partition does not
    /// exist.
    pub async fn delete(&self, partition_id: &str) -> Result<(), Error> {
        self.delete_with_options(partition_id, None).await
    }

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

    /// Cancel Partition
    pub async fn create_partition_cancel(&self, partition_id: &str) -> Result<Partition, Error> {
        self.create_partition_cancel_with_options(partition_id, None)
            .await
    }

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