# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.EnvFile do
  @moduledoc """
  A collection of environment variables.
  """

  use Dagger.Core.Base, kind: :object, name: "EnvFile"

  alias Dagger.Core.Client
  alias Dagger.Core.QueryBuilder, as: QB

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  Return as a file
  """
  @spec as_file(t()) :: Dagger.File.t()
  def as_file(%__MODULE__{} = env_file) do
    query_builder =
      env_file.query_builder |> QB.select("asFile")

    %Dagger.File{
      query_builder: query_builder,
      client: env_file.client
    }
  end

  @doc """
  Check if a variable exists
  """
  @spec exists(t(), String.t()) :: {:ok, boolean()} | {:error, term()}
  def exists(%__MODULE__{} = env_file, name) do
    query_builder =
      env_file.query_builder |> QB.select("exists") |> QB.put_arg("name", name)

    Client.execute(env_file.client, query_builder)
  end

  @doc """
  Lookup a variable (last occurrence wins) and return its value, or an empty string
  """
  @spec get(t(), String.t(), [{:raw, boolean() | nil}]) :: {:ok, String.t()} | {:error, term()}
  def get(%__MODULE__{} = env_file, name, optional_args \\ []) do
    query_builder =
      env_file.query_builder
      |> QB.select("get")
      |> QB.put_arg("name", name)
      |> QB.maybe_put_arg("raw", optional_args[:raw])

    Client.execute(env_file.client, query_builder)
  end

  @doc """
  A unique identifier for this EnvFile.
  """
  @spec id(t()) :: {:ok, Dagger.EnvFileID.t()} | {:error, term()}
  def id(%__MODULE__{} = env_file) do
    query_builder =
      env_file.query_builder |> QB.select("id")

    Client.execute(env_file.client, query_builder)
  end

  @doc """
  Return all variables
  """
  @spec variables(t(), [{:raw, boolean() | nil}]) ::
          {:ok, [Dagger.EnvVariable.t()]} | {:error, term()}
  def variables(%__MODULE__{} = env_file, optional_args \\ []) do
    query_builder =
      env_file.query_builder
      |> QB.select("variables")
      |> QB.maybe_put_arg("raw", optional_args[:raw])
      |> QB.select("id")

    with {:ok, items} <- Client.execute(env_file.client, query_builder) do
      {:ok,
       for %{"id" => id} <- items do
         %Dagger.EnvVariable{
           query_builder:
             QB.query()
             |> QB.select("loadEnvVariableFromID")
             |> QB.put_arg("id", id),
           client: env_file.client
         }
       end}
    end
  end

  @doc """
  Add a variable
  """
  @spec with_variable(t(), String.t(), String.t()) :: Dagger.EnvFile.t()
  def with_variable(%__MODULE__{} = env_file, name, value) do
    query_builder =
      env_file.query_builder
      |> QB.select("withVariable")
      |> QB.put_arg("name", name)
      |> QB.put_arg("value", value)

    %Dagger.EnvFile{
      query_builder: query_builder,
      client: env_file.client
    }
  end

  @doc """
  Remove all occurrences of the named variable
  """
  @spec without_variable(t(), String.t()) :: Dagger.EnvFile.t()
  def without_variable(%__MODULE__{} = env_file, name) do
    query_builder =
      env_file.query_builder |> QB.select("withoutVariable") |> QB.put_arg("name", name)

    %Dagger.EnvFile{
      query_builder: query_builder,
      client: env_file.client
    }
  end
end

defimpl Jason.Encoder, for: Dagger.EnvFile do
  def encode(env_file, opts) do
    {:ok, id} = Dagger.EnvFile.id(env_file)
    Jason.Encode.string(id, opts)
  end
end

defimpl Nestru.Decoder, for: Dagger.EnvFile do
  def decode_fields_hint(_struct, _context, id) do
    {:ok, Dagger.Client.load_env_file_from_id(Dagger.Global.dag(), id)}
  end
end
