defmodule Towerops.DeviceProfiles.SensorState do
  @moduledoc """
  Schema for sensor state definitions.

  Defines enumerated states for state-based sensors (e.g., GPS sync status,
  DFS status, power supply status).
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Towerops.DeviceProfiles.SensorDefinition

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

  schema "profile_sensor_states" do
    field :value, :integer
    field :descr, :string
    field :generic, :integer
    field :graph, :integer

    belongs_to :sensor_definition, SensorDefinition

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(state, attrs) do
    state
    |> cast(attrs, [:value, :descr, :generic, :graph, :sensor_definition_id])
    |> validate_required([:value, :descr, :generic, :graph, :sensor_definition_id])
    |> foreign_key_constraint(:sensor_definition_id)
  end
end
