lib/spatio/api/mail.ex

# NOTE: This file is auto generated by OpenAPI Generator 7.22.0 (https://openapi-generator.tech).
# Do not edit this file manually.

defmodule Spatio.Api.Mail do
  @moduledoc """
  API calls for all endpoints tagged `Mail`.
  """

  alias Spatio.Connection
  import Spatio.RequestBuilder

  @doc """
  Archive multiple messages (remove the INBOX label).

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `bulk_archive_request` (BulkArchiveRequest): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, Spatio.Model.BulkArchiveResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec bulk_archive_emails(Tesla.Env.client, Spatio.Model.BulkArchiveRequest.t, keyword()) :: {:ok, Spatio.Model.BulkArchiveResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def bulk_archive_emails(connection, bulk_archive_request, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/archive")
      |> add_param(:body, :body, bulk_archive_request)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.BulkArchiveResponse},
      {400, Spatio.Model.ApiError},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Delete multiple messages in one call.
  Soft-delete by default (moves to provider trash). Set `permanent: true` for a hard delete. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `bulk_delete_emails_request` (BulkDeleteEmailsRequest): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, Spatio.Model.BulkDeleteEmailsResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec bulk_delete_emails(Tesla.Env.client, Spatio.Model.BulkDeleteEmailsRequest.t, keyword()) :: {:ok, Spatio.Model.BulkDeleteEmailsResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def bulk_delete_emails(connection, bulk_delete_emails_request, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/delete")
      |> add_param(:body, :body, bulk_delete_emails_request)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.BulkDeleteEmailsResponse},
      {400, Spatio.Model.ApiError},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Mark multiple messages read or unread in one call.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `bulk_mark_read_request` (BulkMarkReadRequest): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, Spatio.Model.BulkMarkReadResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec bulk_mark_emails_read(Tesla.Env.client, Spatio.Model.BulkMarkReadRequest.t, keyword()) :: {:ok, Spatio.Model.BulkMarkReadResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def bulk_mark_emails_read(connection, bulk_mark_read_request, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/mark-read")
      |> add_param(:body, :body, bulk_mark_read_request)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.BulkMarkReadResponse},
      {400, Spatio.Model.ApiError},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Create a draft.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `create_draft_request` (CreateDraftRequest): 
  - `opts` (keyword): Optional parameters
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.DraftResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec create_draft(Tesla.Env.client, Spatio.Model.CreateDraftRequest.t, keyword()) :: {:ok, Spatio.Model.DraftResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:error, Tesla.Env.t}
  def create_draft(connection, create_draft_request, opts \\ []) do
    optional_params = %{
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/drafts")
      |> add_param(:body, :body, create_draft_request)
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {201, Spatio.Model.DraftResponse},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Create a label.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `create_label_request` (CreateLabelRequest): 
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.CreateLabelResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec create_email_label(Tesla.Env.client, Spatio.Model.CreateLabelRequest.t, keyword()) :: {:ok, Spatio.Model.CreateLabelResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:error, Tesla.Env.t}
  def create_email_label(connection, create_label_request, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/labels")
      |> add_param(:body, :body, create_label_request)
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {201, Spatio.Model.CreateLabelResponse},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Create a mail template.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec create_mail_template(Tesla.Env.client, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def create_mail_template(connection, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/templates")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {201, %{}},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Delete a draft.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Draft id.
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec delete_draft(Tesla.Env.client, String.t, keyword()) :: {:ok, nil} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def delete_draft(connection, id, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:delete)
      |> url("/v1/mail/drafts/#{id}")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {204, false},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Delete an email.
  Soft-deletes (moves to provider trash).

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Email message id.
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.SuccessFlag.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec delete_email(Tesla.Env.client, String.t, keyword()) :: {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.SuccessFlag.t} | {:error, Tesla.Env.t}
  def delete_email(connection, id, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:delete)
      |> url("/v1/mail/email/#{id}")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.SuccessFlag},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Delete a label.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Label id.
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec delete_email_label(Tesla.Env.client, String.t, keyword()) :: {:ok, nil} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def delete_email_label(connection, id, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:delete)
      |> url("/v1/mail/labels/#{id}")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {204, false},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Delete a mail template.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec delete_mail_template(Tesla.Env.client, String.t, keyword()) :: {:ok, nil} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def delete_mail_template(connection, id, _opts \\ []) do
    request =
      %{}
      |> method(:delete)
      |> url("/v1/mail/templates/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {204, false},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Fetch one email.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Email message id.
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.GetEmailResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec get_email(Tesla.Env.client, String.t, keyword()) :: {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.GetEmailResponse.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:error, Tesla.Env.t}
  def get_email(connection, id, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/email/#{id}")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.GetEmailResponse},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Download an attachment.
  Streams the attachment binary. Response `Content-Type` matches the attachment's declared MIME type; `Content-Disposition` sets the filename. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `message_id` (String.t): Message id the attachment belongs to.
  - `attachment_id` (String.t): Attachment id within the message.
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, String.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec get_email_attachment(Tesla.Env.client, String.t, String.t, keyword()) :: {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:ok, String.t} | {:error, Tesla.Env.t}
  def get_email_attachment(connection, message_id, attachment_id, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/attachment/#{message_id}/#{attachment_id}")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, false},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Fetch a thread (the conversation a message belongs to).

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Thread id.
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.GetThreadResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec get_email_thread(Tesla.Env.client, String.t, keyword()) :: {:ok, Spatio.Model.GetThreadResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:error, Tesla.Env.t}
  def get_email_thread(connection, id, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/thread/#{id}")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.GetThreadResponse},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Fetch a mail template.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec get_mail_template(Tesla.Env.client, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def get_mail_template(connection, id, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/templates/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Read mail-tracking events for a thread (open log, reply log, etc.).

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `thread_id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec get_mail_thread_tracking(Tesla.Env.client, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def get_mail_thread_tracking(connection, thread_id, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/threads/#{thread_id}/tracking")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Render a template with variables and return the resulting draft.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec instantiate_mail_template(Tesla.Env.client, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def instantiate_mail_template(connection, id, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/templates/#{id}/instantiate")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  List drafts across connected mail accounts.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `opts` (keyword): Optional parameters
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 
    - `:accountIds` ([String.t]): Repeatable. Restrict to these connected-account row ids. Mutually orthogonal to `providers` — when both are set the intersection is used. 
    - `:providers` ([String.t]): Repeatable. Restrict to these provider ids (`gmail`, `outlook`).
    - `:limit` (integer()): 
    - `:nextPageToken` (String.t): 

  ### Returns

  - `{:ok, Spatio.Model.ListDraftsResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec list_drafts(Tesla.Env.client, keyword()) :: {:ok, Spatio.Model.ListDraftsResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def list_drafts(connection, opts \\ []) do
    optional_params = %{
      :"X-Workspace-ID" => :headers,
      :accountIds => :query,
      :providers => :query,
      :limit => :query,
      :nextPageToken => :query
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/drafts")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.ListDraftsResponse},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  List labels on the resolved mail account.
  Single-account list. The platform auto-resolves to the caller's sole connected account; pass `?accountId=` to disambiguate when multiple are connected. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.ListLabelsResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec list_email_labels(Tesla.Env.client, keyword()) :: {:ok, Spatio.Model.ListLabelsResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:error, Tesla.Env.t}
  def list_email_labels(connection, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/labels")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.ListLabelsResponse},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  List emails across connected mail accounts.
  Fan-out list. Returns messages across every connected mail provider unless filtered. Pass `?accountIds=` (repeatable) to restrict to specific accounts, `?providers=` to restrict to specific provider ids, or both for the intersection. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `opts` (keyword): Optional parameters
    - `:accountIds` ([String.t]): Repeatable. Restrict to these connected-account row ids. Mutually orthogonal to `providers` — when both are set the intersection is used. 
    - `:providers` ([String.t]): Repeatable. Restrict to these provider ids (`gmail`, `outlook`).
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 
    - `:query` (String.t): Provider-specific full-text query (e.g. Gmail search syntax).
    - `:labels` ([String.t]): Repeatable. Filter to messages carrying every label.
    - `:folder` (String.t): Logical folder filter. Canonical values: `inbox`, `sent`, `starred`, `trash`, `archive`. Provider-specific folders accepted as opaque strings. 
    - `:limit` (integer()): 
    - `:offset` (integer()): 

  ### Returns

  - `{:ok, Spatio.Model.ListEmailsResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec list_emails(Tesla.Env.client, keyword()) :: {:ok, Spatio.Model.ListEmailsResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def list_emails(connection, opts \\ []) do
    optional_params = %{
      :accountIds => :query,
      :providers => :query,
      :"X-Workspace-ID" => :headers,
      :query => :query,
      :labels => :query,
      :folder => :query,
      :limit => :query,
      :offset => :query
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/list")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.ListEmailsResponse},
      {401, Spatio.Model.ApiError},
      {500, Spatio.Model.ApiError}
    ])
  end

  @doc """
  List the caller's saved mail templates.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec list_mail_templates(Tesla.Env.client, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def list_mail_templates(connection, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/templates")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Reply to a specific email.
  The original message is identified by `?messageId=`. Body defaults to the original sender as recipient — pass `to`, `cc`, `bcc` to override. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `message_id` (String.t): Id of the message being replied to.
  - `reply_email_request` (ReplyEmailRequest): 
  - `opts` (keyword): Optional parameters
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.SendEmailResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec reply_email(Tesla.Env.client, String.t, Spatio.Model.ReplyEmailRequest.t, keyword()) :: {:ok, Spatio.Model.SendEmailResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:error, Tesla.Env.t}
  def reply_email(connection, message_id, reply_email_request, opts \\ []) do
    optional_params = %{
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/reply")
      |> add_param(:query, :messageId, message_id)
      |> add_param(:body, :body, reply_email_request)
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.SendEmailResponse},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Save-or-create endpoint used by the renderer's \"save as template\" flow. Distinct from POST /v1/mail/templates which is the strict create. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec save_mail_template(Tesla.Env.client, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def save_mail_template(connection, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/templates/save")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Structured search across connected mail accounts.
  Fan-out search. Mirrors `listEmails`'s account/provider filter semantics. Date range filters are inclusive. The query string itself is passed via `?q=` (not `?query=`); structured filters go in their own params. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `q` (String.t): Provider-specific full-text query string.
  - `opts` (keyword): Optional parameters
    - `:accountIds` ([String.t]): Repeatable. Restrict to these connected-account row ids. Mutually orthogonal to `providers` — when both are set the intersection is used. 
    - `:providers` ([String.t]): Repeatable. Restrict to these provider ids (`gmail`, `outlook`).
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 
    - `:from` (String.t): 
    - `:to` (String.t): 
    - `:subject` (String.t): 
    - `:hasAttachment` (boolean()): 
    - `:isUnread` (boolean()): 
    - `:isStarred` (boolean()): 
    - `:labels` ([String.t]): 
    - `:after` (DateTime.t): Inclusive lower-bound date.
    - `:before` (DateTime.t): Inclusive upper-bound date.
    - `:limit` (integer()): 
    - `:nextPageToken` (String.t): Cursor returned by the previous call.

  ### Returns

  - `{:ok, Spatio.Model.SearchEmailsResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec search_emails(Tesla.Env.client, String.t, keyword()) :: {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.SearchEmailsResponse.t} | {:error, Tesla.Env.t}
  def search_emails(connection, q, opts \\ []) do
    optional_params = %{
      :accountIds => :query,
      :providers => :query,
      :"X-Workspace-ID" => :headers,
      :from => :query,
      :to => :query,
      :subject => :query,
      :hasAttachment => :query,
      :isUnread => :query,
      :isStarred => :query,
      :labels => :query,
      :after => :query,
      :before => :query,
      :limit => :query,
      :nextPageToken => :query
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/mail/search")
      |> add_param(:query, :q, q)
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.SearchEmailsResponse},
      {401, Spatio.Model.ApiError},
      {500, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Send a draft.
  Submits the draft as an outbound message. The draft is consumed by the provider — subsequent `getDraft`/`updateDraft` calls return `404`. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Draft id.
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.SendEmailResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec send_draft(Tesla.Env.client, String.t, keyword()) :: {:ok, Spatio.Model.SendEmailResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def send_draft(connection, id, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/drafts/#{id}/send")
      |> add_optional_params(optional_params, opts)
      |> ensure_body()
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.SendEmailResponse},
      {400, Spatio.Model.ApiError},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Send an email.
  Sends through the resolved connected account (auto-picks if the caller has exactly one connected mail account; errors `ambiguous_account` otherwise unless `accountId` is supplied). 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `send_email_request` (SendEmailRequest): 
  - `opts` (keyword): Optional parameters
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.SendEmailResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec send_email(Tesla.Env.client, Spatio.Model.SendEmailRequest.t, keyword()) :: {:ok, Spatio.Model.SendEmailResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:ok, Spatio.Model.CreateNote400Response.t} | {:error, Tesla.Env.t}
  def send_email(connection, send_email_request, opts \\ []) do
    optional_params = %{
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:post)
      |> url("/v1/mail/send")
      |> add_param(:body, :body, send_email_request)
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.SendEmailResponse},
      {400, Spatio.Model.CreateNote400Response},
      {401, Spatio.Model.ApiError},
      {500, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Update a draft (full replacement of provided fields).
  PUT replaces the full set of provided fields on the draft. Fields omitted from the body are not modified. 

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Draft id.
  - `update_draft_request` (UpdateDraftRequest): 
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.DraftResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec update_draft(Tesla.Env.client, String.t, Spatio.Model.UpdateDraftRequest.t, keyword()) :: {:ok, Spatio.Model.DraftResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def update_draft(connection, id, update_draft_request, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:put)
      |> url("/v1/mail/drafts/#{id}")
      |> add_param(:body, :body, update_draft_request)
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.DraftResponse},
      {400, Spatio.Model.ApiError},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Update an email (mark read/star, add/remove labels).

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): Email message id.
  - `update_email_request` (UpdateEmailRequest): 
  - `opts` (keyword): Optional parameters
    - `:accountId` (String.t): Connected-account row id. Selects which provider account this request targets when more than one is connected. Mutually exclusive with `provider`. If omitted on a list endpoint the call fans out across every connected account. 
    - `:"X-Workspace-ID"` (String.t): Workspace scope for unscoped tokens. Workspace-scoped PATs and OAuth tokens carry this implicitly; for session/JWT auth without a scoped PAT, pass it explicitly. 

  ### Returns

  - `{:ok, Spatio.Model.UpdateEmailResponse.t}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec update_email(Tesla.Env.client, String.t, Spatio.Model.UpdateEmailRequest.t, keyword()) :: {:ok, Spatio.Model.UpdateEmailResponse.t} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def update_email(connection, id, update_email_request, opts \\ []) do
    optional_params = %{
      :accountId => :query,
      :"X-Workspace-ID" => :headers
    }

    request =
      %{}
      |> method(:patch)
      |> url("/v1/mail/email/#{id}")
      |> add_param(:body, :body, update_email_request)
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, Spatio.Model.UpdateEmailResponse},
      {400, Spatio.Model.ApiError},
      {401, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Update a mail template.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `id` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec update_mail_template(Tesla.Env.client, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def update_mail_template(connection, id, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:patch)
      |> url("/v1/mail/templates/#{id}")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `message_id` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_add_mail_message_labels(Tesla.Env.client, String.t, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_add_mail_message_labels(connection, org, workspace, message_id, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/#{message_id}/labels")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_create_mail_draft(Tesla.Env.client, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_create_mail_draft(connection, org, workspace, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/drafts")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {201, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_create_mail_label(Tesla.Env.client, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_create_mail_label(connection, org, workspace, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/labels")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {201, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_delete_mail(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, nil} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_delete_mail(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:delete)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/email/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {204, false},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_delete_mail_draft(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, nil} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_delete_mail_draft(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:delete)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/drafts/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {204, false},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_delete_mail_label(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, nil} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_delete_mail_label(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:delete)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/labels/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {204, false},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_get_mail(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_get_mail(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/email/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError},
      {404, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `message_id` (String.t): 
  - `attachment_id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_get_mail_attachment(Tesla.Env.client, String.t, String.t, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_get_mail_attachment(connection, org, workspace, message_id, attachment_id, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/attachment/#{message_id}/#{attachment_id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Workspace-scoped renderer-compat alias for mail/email/{id}.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_get_mail_by_id(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_get_mail_by_id(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_get_mail_draft(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_get_mail_draft(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/drafts/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_get_mail_thread(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_get_mail_thread(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/thread/#{id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_list_mail(Tesla.Env.client, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_list_mail(connection, org, workspace, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/list")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_list_mail_drafts(Tesla.Env.client, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_list_mail_drafts(connection, org, workspace, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/drafts")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_list_mail_labels(Tesla.Env.client, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_list_mail_labels(connection, org, workspace, _opts \\ []) do
    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/labels")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_patch_mail(Tesla.Env.client, String.t, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_patch_mail(connection, org, workspace, id, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:patch)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/email/#{id}")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `message_id` (String.t): 
  - `label_id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_remove_mail_message_label(Tesla.Env.client, String.t, String.t, String.t, String.t, keyword()) :: {:ok, nil} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_remove_mail_message_label(connection, org, workspace, message_id, label_id, _opts \\ []) do
    request =
      %{}
      |> method(:delete)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/#{message_id}/labels/#{label_id}")
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {204, false},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_reply_mail(Tesla.Env.client, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_reply_mail(connection, org, workspace, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/reply")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `opts` (keyword): Optional parameters
    - `:q` (String.t): 

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_search_mail(Tesla.Env.client, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_search_mail(connection, org, workspace, opts \\ []) do
    optional_params = %{
      :q => :query
    }

    request =
      %{}
      |> method(:get)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/search")
      |> add_optional_params(optional_params, opts)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_send_mail(Tesla.Env.client, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_send_mail(connection, org, workspace, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/send")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_send_mail_draft(Tesla.Env.client, String.t, String.t, String.t, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_send_mail_draft(connection, org, workspace, id, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/drafts/#{id}/send")
      |> ensure_body()
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """
  Renderer-compat alias for /mail/send.

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_send_mail_email_alias(Tesla.Env.client, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_send_mail_email_alias(connection, org, workspace, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/email")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_update_mail(Tesla.Env.client, String.t, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_update_mail(connection, org, workspace, id, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:put)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/email/#{id}")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_update_mail_draft(Tesla.Env.client, String.t, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_update_mail_draft(connection, org, workspace, id, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:put)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/drafts/#{id}")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end

  @doc """

  ### Parameters

  - `connection` (Spatio.Connection): Connection to server
  - `org` (String.t): 
  - `workspace` (String.t): 
  - `id` (String.t): 
  - `request_body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, %{}}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec workspace_update_mail_label(Tesla.Env.client, String.t, String.t, String.t, %{optional(String.t) => any()}, keyword()) :: {:ok, %{optional(String.t) => any()}} | {:ok, Spatio.Model.ApiError.t} | {:error, Tesla.Env.t}
  def workspace_update_mail_label(connection, org, workspace, id, request_body, _opts \\ []) do
    request =
      %{}
      |> method(:put)
      |> url("/v1/organizations/#{org}/workspaces/#{workspace}/mail/labels/#{id}")
      |> add_param(:body, :body, request_body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, %{}},
      {401, Spatio.Model.ApiError},
      {403, Spatio.Model.ApiError}
    ])
  end
end