// 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 FilesApi<'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>,
    /// Items per page
    ///
    /// 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<FilesOrder>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<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>,
    /// Include embeddings in the response
    ///
    /// Defaults to `false`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_embeddings: Option<bool>,
    /// Defaults to `created_at`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort_by: Option<String>,
}

impl Default for ListParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            before: Default::default(),
            after: Default::default(),
            limit: Some(10),
            order: Some(FilesOrder::Desc),
            filename: Default::default(),
            mime_type: Default::default(),
            from_date: Default::default(),
            to_date: Default::default(),
            include_embeddings: Some(false),
            sort_by: Some("created_at".to_string()),
        }
    }
}

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

impl CreateBlueprintParams {
    /// Construct a new `CreateBlueprintParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: CreateFileBlueprintRequest) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct GetBlueprintParams {
    /// 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 GetBlueprintParams {
    #[allow(deprecated)]
    fn default() -> Self {
        Self {
            include_output: Some(true),
        }
    }
}

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

impl CreateUploadParams {
    /// Construct a new `CreateUploadParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: UploadFileRequest) -> Self {
        Self { body }
    }
}

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

impl CompleteUploadParams {
    /// Construct a new `CompleteUploadParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: CompleteFileUploadRequest) -> Self {
        Self { body }
    }
}

impl<'a> FilesApi<'a> {
    /// List Files
    ///
    /// List files with pagination and optional filtering.
    pub async fn list(&self, params: ListParams) -> Result<FileList, 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<FileList, Error> {
        let path = "/v1/files".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create File Blueprint
    ///
    /// Create a Document Blueprint for an uploaded file.
    pub async fn create_blueprint(
        &self,
        params: CreateBlueprintParams,
    ) -> Result<FileBlueprint, Error> {
        self.create_blueprint_with_options(params, None).await
    }

    /// Variant of [`Self::create_blueprint`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_blueprint_with_options(
        &self,
        params: CreateBlueprintParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<FileBlueprint, Error> {
        let path = "/v1/files/blueprints".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get File Blueprint
    ///
    /// Retrieve a file blueprint by id.
    pub async fn get_blueprint(
        &self,
        blueprint_id: &str,
        params: GetBlueprintParams,
    ) -> Result<FileBlueprint, Error> {
        self.get_blueprint_with_options(blueprint_id, params, None)
            .await
    }

    /// Variant of [`Self::get_blueprint`] that accepts per-request [`crate::RequestOptions`].
    pub async fn get_blueprint_with_options(
        &self,
        blueprint_id: &str,
        params: GetBlueprintParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<FileBlueprint, Error> {
        let blueprint_id = crate::client::path_segment(blueprint_id);
        let path = format!("/v1/files/blueprints/{blueprint_id}");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Cancel File Blueprint
    ///
    /// Cancel an in-flight background file blueprint.
    pub async fn create_blueprint_cancel(
        &self,
        blueprint_id: &str,
    ) -> Result<FileBlueprint, Error> {
        self.create_blueprint_cancel_with_options(blueprint_id, None)
            .await
    }

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

    /// Upload File
    ///
    /// Start a file upload.
    ///
    /// Reserves a file record for the given `filename`, `content_type`, and
    /// `size_bytes`, and returns a short-lived signed `upload_url` the client uses
    /// to `PUT` the file content directly. Call the complete-upload endpoint with
    /// the returned `file_id` once the bytes have been uploaded.
    pub async fn create_upload(
        &self,
        params: CreateUploadParams,
    ) -> Result<CreateUploadResponse, Error> {
        self.create_upload_with_options(params, None).await
    }

    /// Variant of [`Self::create_upload`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_upload_with_options(
        &self,
        params: CreateUploadParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<CreateUploadResponse, Error> {
        let path = "/v1/files/upload".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Complete Upload File
    ///
    /// Finalize a file upload.
    ///
    /// Confirms that the content for `file_id` has been uploaded, verifying the
    /// object's size and optional `sha256` checksum against the upload session,
    /// and marks the file ready. Returns a durable reference to the stored file.
    /// Responds with `404` if the upload session is unknown, `410` if it has
    /// expired, and `422` if the size or checksum does not match.
    pub async fn complete_upload(
        &self,
        file_id: &str,
        params: CompleteUploadParams,
    ) -> Result<MimeData, Error> {
        self.complete_upload_with_options(file_id, params, None)
            .await
    }

    /// Variant of [`Self::complete_upload`] that accepts per-request [`crate::RequestOptions`].
    pub async fn complete_upload_with_options(
        &self,
        file_id: &str,
        params: CompleteUploadParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<MimeData, Error> {
        let file_id = crate::client::path_segment(file_id);
        let path = format!("/v1/files/upload/{file_id}/complete");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get File
    ///
    /// Retrieve a file.
    ///
    /// Returns metadata for the file identified by `file_id`, including its
    /// `filename`, `page_count`, and timestamps. Responds with `404` if no
    /// matching file exists.
    pub async fn get(&self, file_id: &str) -> Result<File, Error> {
        self.get_with_options(file_id, None).await
    }

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

    /// Download Link
    ///
    /// Get a temporary download link for a file.
    ///
    /// Returns a short-lived signed `download_url` for the file identified by
    /// `file_id`, along with its `filename` and expiration. Responds with `404`
    /// if no matching file exists.
    pub async fn get_download_link(&self, file_id: &str) -> Result<FileLink, Error> {
        self.get_download_link_with_options(file_id, None).await
    }

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