defmodule Towerops.Snmp.ArpEntry do
  @moduledoc """
  SNMP ARP entry schema for IP-to-MAC address mappings.

  Stores ARP table entries discovered via ipNetToMediaTable (1.3.6.1.2.1.4.22).
  """
  use Ecto.Schema

  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "snmp_arp_entries" do
    field :ip_address, :string
    field :mac_address, :string
    field :entry_type, :string
    field :if_index, :integer
    field :last_seen_at, :utc_datetime

    belongs_to :device, Towerops.Devices.Device
    belongs_to :interface, Towerops.Snmp.Interface

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(arp_entry, attrs) do
    arp_entry
    |> cast(attrs, [
      :device_id,
      :interface_id,
      :ip_address,
      :mac_address,
      :entry_type,
      :if_index,
      :last_seen_at
    ])
    |> validate_required([
      :device_id,
      :ip_address,
      :mac_address,
      :last_seen_at
    ])
    |> foreign_key_constraint(:device_id)
    |> foreign_key_constraint(:interface_id)
    |> unique_constraint([:device_id, :ip_address, :mac_address])
  end
end
