// 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 crate::resources::edit_templates::EditTemplatesApi;
#[allow(unused_imports)]
use serde::Serialize;

pub struct EditsApi<'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<EditsOrder>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub template_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<EditsStatus>,
    #[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(EditsOrder::Desc),
            filename: Default::default(),
            template_id: 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: EditRequest,
}

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(instructions: impl Into<String>) -> Self {
        Self {
            body: EditRequest {
                instructions: instructions.into(),
                document: Default::default(),
                template_id: Default::default(),
                model: Default::default(),
                config: 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> EditsApi<'a> {
    /// Access the `templates` sub-resource.
    pub fn templates(&self) -> EditTemplatesApi<'a> {
        EditTemplatesApi {
            client: self.client,
        }
    }

    /// List Edits
    ///
    /// List edits.
    ///
    /// Returns a paginated list of edits. Filter by `filename` (case-insensitive
    /// prefix match), `template_id`, and a `from_date`/`to_date` creation range
    /// (each `YYYY-MM-DD`). Page with `before`/`after` cursors, `limit`, and
    /// `order`; an invalid date format responds with `400`.
    pub async fn list(&self, params: ListParams) -> Result<EditList, 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<EditList, Error> {
        let path = "/v1/edits".to_string();
        let method = http::Method::GET;
        self.client
            .request_page(method, &path, &params, "after", options)
            .await
    }

    /// Create Edit
    ///
    /// Create an edit.
    ///
    /// Fills the form fields of a document according to `instructions` and renders
    /// the values into a PDF. Provide exactly one of `document` (a PDF, DOCX, XLSX,
    /// or PPTX) or `template_id` (an existing edit template) — supplying both or
    /// neither responds with `400`. Returns the created edit with the filled form
    /// data and rendered document; responds with `201`.
    pub async fn create(&self, params: CreateParams) -> Result<Edit, 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<Edit, Error> {
        let path = "/v1/edits".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Edit
    ///
    /// Retrieve an edit.
    ///
    /// Fetches a single edit by its `edit_id`. Returns the edit with its filled
    /// form data and rendered document; responds with `404` if no edit with that
    /// id exists.
    pub async fn get(&self, edit_id: &str, params: GetParams) -> Result<Edit, Error> {
        self.get_with_options(edit_id, params, None).await
    }

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

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

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

    /// Cancel Edit
    pub async fn create_edit_cancel(&self, edit_id: &str) -> Result<Edit, Error> {
        self.create_edit_cancel_with_options(edit_id, None).await
    }

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