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

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

impl UploadParams {
    /// Construct a new `UploadParams` 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 }
    }
}

#[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()),
        }
    }
}

impl<'a> FilesApi<'a> {
    /// Upload File
    pub async fn upload(&self, params: UploadParams) -> Result<CreateUploadResponse, Error> {
        self.upload_with_options(params, None).await
    }

    /// Variant of [`Self::upload`] that accepts per-request [`crate::RequestOptions`].
    pub async fn upload_with_options(
        &self,
        params: UploadParams,
        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
    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
    }

    /// 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_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Returns an async [`futures_util::Stream`] that yields every `File`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<File> = self
    ///     .list_auto_paging(params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_auto_paging(
        &self,
        params: ListParams,
    ) -> impl futures_util::Stream<Item = Result<File, 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))
            }
        })
    }

    /// Get File
    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
    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
    }
}
