defmodule Towerops.Snmp.Device do
  @moduledoc """
  SNMP device schema containing discovered device information.

  Stores system details (sysDescr, sysObjectID), vendor information,
  and relationships to interfaces, sensors, and neighbors.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Ecto.Association.NotLoaded
  alias Towerops.Devices.Device, as: DeviceSchema
  alias Towerops.Snmp.Interface
  alias Towerops.Snmp.Sensor

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "snmp_devices" do
    field :sys_descr, :string
    field :sys_object_id, :string
    field :sys_name, :string
    field :sys_uptime, :integer
    field :sys_contact, :string
    field :sys_location, :string
    field :manufacturer, :string
    field :model, :string
    field :firmware_version, :string
    field :serial_number, :string

    belongs_to :device, DeviceSchema

    has_many :interfaces, Interface, foreign_key: :snmp_device_id
    has_many :sensors, Sensor, foreign_key: :snmp_device_id

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{
          id: Ecto.UUID.t(),
          sys_descr: String.t() | nil,
          sys_object_id: String.t() | nil,
          sys_name: String.t() | nil,
          sys_uptime: integer() | nil,
          sys_contact: String.t() | nil,
          sys_location: String.t() | nil,
          manufacturer: String.t() | nil,
          model: String.t() | nil,
          firmware_version: String.t() | nil,
          serial_number: String.t() | nil,
          device_id: Ecto.UUID.t(),
          device: NotLoaded.t() | Devices.t(),
          interfaces: NotLoaded.t() | [Interface.t()],
          sensors: NotLoaded.t() | [Sensor.t()],
          inserted_at: DateTime.t(),
          updated_at: DateTime.t()
        }

  @doc false
  def changeset(device, attrs) do
    device
    |> cast(attrs, [
      :device_id,
      :sys_descr,
      :sys_object_id,
      :sys_name,
      :sys_uptime,
      :sys_contact,
      :sys_location,
      :manufacturer,
      :model,
      :firmware_version,
      :serial_number
    ])
    |> validate_required([:device_id])
    |> unique_constraint(:device_id)
    |> foreign_key_constraint(:device_id)
  end
end
