defmodule Towerops.DeviceProfiles.DetectionRule do
  @moduledoc """
  Schema for device detection rules.

  Defines rules for identifying which device profile to use based on
  sysObjectID, sysDescr patterns, and SNMP queries.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Towerops.DeviceProfiles.DeviceProfile

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

  schema "profile_detection_rules" do
    field :rule_type, :string
    field :oid, :string
    field :operator, :string
    field :value, :string
    field :pattern, :string
    field :priority, :integer, default: 0

    belongs_to :device_profile, DeviceProfile

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(rule, attrs) do
    rule
    |> cast(attrs, [:rule_type, :oid, :operator, :value, :pattern, :priority, :device_profile_id])
    |> validate_required([:rule_type, :device_profile_id])
    |> foreign_key_constraint(:device_profile_id)
  end
end
