defmodule Microwaveprop.Terrain.TerrainProfile do
  @moduledoc false
  use Ecto.Schema

  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "terrain_profiles" do
    belongs_to :contact, Microwaveprop.Radio.Contact, foreign_key: :contact_id
    field :sample_count, :integer
    field :path_points, {:array, :map}
    field :max_elevation_m, :float
    field :min_clearance_m, :float
    field :diffraction_db, :float
    field :fresnel_hit_count, :integer
    field :obstructed_count, :integer
    field :verdict, :string

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(contact_id sample_count path_points verdict)a
  @optional_fields ~w(max_elevation_m min_clearance_m diffraction_db fresnel_hit_count obstructed_count)a

  @spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
  def changeset(profile, attrs) do
    profile
    |> cast(attrs, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
    |> foreign_key_constraint(:contact_id)
    |> unique_constraint([:contact_id])
  end
end
