defmodule Microwaveprop.Terrain do
  @moduledoc false

  import Ecto.Query

  alias Microwaveprop.Repo
  alias Microwaveprop.Terrain.TerrainProfile

  @spec upsert_terrain_profile(map()) :: {:ok, TerrainProfile.t()} | {:error, Ecto.Changeset.t()}
  def upsert_terrain_profile(attrs) do
    %TerrainProfile{}
    |> TerrainProfile.changeset(attrs)
    |> Repo.insert(
      on_conflict: :nothing,
      conflict_target: [:contact_id]
    )
  end

  @spec get_terrain_profile(Ecto.UUID.t()) :: TerrainProfile.t() | nil
  def get_terrain_profile(contact_id) do
    Repo.get_by(TerrainProfile, contact_id: contact_id)
  end

  @spec has_terrain_profile?(Ecto.UUID.t()) :: boolean()
  def has_terrain_profile?(contact_id) do
    TerrainProfile
    |> where([t], t.contact_id == ^contact_id)
    |> Repo.exists?()
  end
end
