// 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 ExtractionsApi<'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<ExtractionsOrder>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,
    /// Deprecated alias for prefix filename filtering. Regex patterns are rejected.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename_regex: Option<String>,
    /// Plain text filename text search powered by Atlas Search when available.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename_contains: Option<String>,
    /// Filter by document type. Can be repeated. Accepted values: bmp, csv, doc, docm, docx, dotm, dotx, eml, gif, heic, heif, htm, html, jpeg, jpg, json, md, mhtml, msg, odp, ods, odt, ots, ott, pdf, png, ppt, pptx, rtf, svg, tif, tiff, tsv, txt, webp, xlam, xls, xlsb, xlsm, xlsx, xltm, xltx, xml, yaml, yml.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document_type: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_date: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_date: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<String>,
}

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

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

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,
        json_schema: std::collections::HashMap<String, serde_json::Value>,
    ) -> Self {
        Self {
            body: ExtractionRequest {
                document: ClassificationRequestDocumentOneOf::MimeData(Box::new(document.into())),
                json_schema,
                model: Default::default(),
                image_resolution_dpi: Default::default(),
                instructions: Default::default(),
                n_consensus: Default::default(),
                metadata: Default::default(),
                additional_messages: Default::default(),
                bust_cache: Default::default(),
                stream: Default::default(),
                chunking_keys: Default::default(),
            },
        }
    }
}

impl<'a> ExtractionsApi<'a> {
    /// List Extractions
    ///
    /// List and paginate extractions with optional filtering.
    ///
    /// Returns a paginated list of extraction documents matching the filter criteria.
    ///
    /// The `metadata` parameter accepts a JSON string of key-value pairs to filter by.
    pub async fn list(&self, params: ListParams) -> Result<ExtractionList, 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<ExtractionList, Error> {
        let path = "/v1/extractions".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Extraction
    pub async fn create(&self, params: CreateParams) -> Result<Extraction, 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<Extraction, Error> {
        let path = "/v1/extractions".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Extraction
    pub async fn get(&self, extraction_id: &str) -> Result<Extraction, Error> {
        self.get_with_options(extraction_id, None).await
    }

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

    /// Delete Extraction
    ///
    /// Delete an extraction
    pub async fn delete(&self, extraction_id: &str) -> Result<(), Error> {
        self.delete_with_options(extraction_id, None).await
    }

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

    /// Get Extraction Sources
    ///
    /// Return the extraction result enriched with per-leaf source provenance.
    ///
    /// Each extracted leaf value is wrapped as {value, source} where source
    /// contains citation content, surrounding context, and a format-specific
    /// anchor (bbox for PDFs, cell ref for spreadsheets, text span for plain text, etc.).
    pub async fn sources(&self, extraction_id: &str) -> Result<SourcesResponse, Error> {
        self.sources_with_options(extraction_id, None).await
    }

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