# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.GitRepository do
  @moduledoc """
  A git repository.
  """

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

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

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  Returns details of a branch.
  """
  @spec branch(t(), String.t()) :: Dagger.GitRef.t()
  def branch(%__MODULE__{} = git_repository, name) do
    query_builder =
      git_repository.query_builder |> QB.select("branch") |> QB.put_arg("name", name)

    %Dagger.GitRef{
      query_builder: query_builder,
      client: git_repository.client
    }
  end

  @doc """
  branches that match any of the given glob patterns.
  """
  @spec branches(t(), [{:patterns, [String.t()]}]) :: {:ok, [String.t()]} | {:error, term()}
  def branches(%__MODULE__{} = git_repository, optional_args \\ []) do
    query_builder =
      git_repository.query_builder
      |> QB.select("branches")
      |> QB.maybe_put_arg("patterns", optional_args[:patterns])

    Client.execute(git_repository.client, query_builder)
  end

  @doc """
  Returns details of a commit.
  """
  @spec commit(t(), String.t()) :: Dagger.GitRef.t()
  def commit(%__MODULE__{} = git_repository, id) do
    query_builder =
      git_repository.query_builder |> QB.select("commit") |> QB.put_arg("id", id)

    %Dagger.GitRef{
      query_builder: query_builder,
      client: git_repository.client
    }
  end

  @doc """
  Returns details for HEAD.
  """
  @spec head(t()) :: Dagger.GitRef.t()
  def head(%__MODULE__{} = git_repository) do
    query_builder =
      git_repository.query_builder |> QB.select("head")

    %Dagger.GitRef{
      query_builder: query_builder,
      client: git_repository.client
    }
  end

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

    Client.execute(git_repository.client, query_builder)
  end

  @doc """
  Returns details for the latest semver tag.
  """
  @spec latest_version(t()) :: Dagger.GitRef.t()
  def latest_version(%__MODULE__{} = git_repository) do
    query_builder =
      git_repository.query_builder |> QB.select("latestVersion")

    %Dagger.GitRef{
      query_builder: query_builder,
      client: git_repository.client
    }
  end

  @doc """
  Returns details of a ref.
  """
  @spec ref(t(), String.t()) :: Dagger.GitRef.t()
  def ref(%__MODULE__{} = git_repository, name) do
    query_builder =
      git_repository.query_builder |> QB.select("ref") |> QB.put_arg("name", name)

    %Dagger.GitRef{
      query_builder: query_builder,
      client: git_repository.client
    }
  end

  @doc """
  Returns details of a tag.
  """
  @spec tag(t(), String.t()) :: Dagger.GitRef.t()
  def tag(%__MODULE__{} = git_repository, name) do
    query_builder =
      git_repository.query_builder |> QB.select("tag") |> QB.put_arg("name", name)

    %Dagger.GitRef{
      query_builder: query_builder,
      client: git_repository.client
    }
  end

  @doc """
  tags that match any of the given glob patterns.
  """
  @spec tags(t(), [{:patterns, [String.t()]}]) :: {:ok, [String.t()]} | {:error, term()}
  def tags(%__MODULE__{} = git_repository, optional_args \\ []) do
    query_builder =
      git_repository.query_builder
      |> QB.select("tags")
      |> QB.maybe_put_arg("patterns", optional_args[:patterns])

    Client.execute(git_repository.client, query_builder)
  end

  @doc """
  The URL of the git repository.
  """
  @spec url(t()) :: {:ok, String.t() | nil} | {:error, term()}
  def url(%__MODULE__{} = git_repository) do
    query_builder =
      git_repository.query_builder |> QB.select("url")

    Client.execute(git_repository.client, query_builder)
  end
end

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

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