defmodule Towerops.DeviceProfiles.DeviceProfile do
  @moduledoc """
  Schema for device SNMP profiles.

  Stores high-level device type information and coordinates detection rules,
  sensor definitions, and other device-specific SNMP configurations.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Towerops.DeviceProfiles.DetectionRule
  alias Towerops.DeviceProfiles.MemPoolDefinition
  alias Towerops.DeviceProfiles.OSDefinition
  alias Towerops.DeviceProfiles.ProcessorDefinition
  alias Towerops.DeviceProfiles.SensorDefinition

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

  schema "device_profiles" do
    field :os, :string
    field :text, :string
    field :type, :string
    field :icon, :string
    field :group, :string
    field :mib, :string
    field :snmp_bulk, :boolean, default: true
    field :mib_dir, :string
    field :enabled, :boolean, default: true
    field :priority, :integer, default: 100

    has_many :detection_rules, DetectionRule, foreign_key: :device_profile_id
    has_many :sensor_definitions, SensorDefinition, foreign_key: :device_profile_id
    has_many :processor_definitions, ProcessorDefinition, foreign_key: :device_profile_id
    has_many :mempool_definitions, MemPoolDefinition, foreign_key: :device_profile_id
    has_many :os_definitions, OSDefinition, foreign_key: :device_profile_id

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(profile, attrs) do
    profile
    |> cast(attrs, [
      :os,
      :text,
      :type,
      :icon,
      :group,
      :mib,
      :snmp_bulk,
      :mib_dir,
      :enabled,
      :priority
    ])
    |> validate_required([:os, :text])
    |> unique_constraint(:os)
  end
end
