defmodule Towerops.Repo.Migrations.CreateSnmpPhysicalEntities do
  use Ecto.Migration

  def change do
    create table(:snmp_physical_entities, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :snmp_device_id,
          references(:snmp_devices, type: :binary_id, on_delete: :delete_all),
          null: false

      add :entity_index, :integer, null: false
      add :entity_class, :string, null: false
      add :description, :string
      add :model_name, :string
      add :serial_number, :string
      add :firmware_revision, :string
      add :hardware_revision, :string
      add :manufacturer_name, :string
      add :position, :integer
      add :last_checked_at, :utc_datetime
      add :metadata, :map, default: %{}

      # Self-referencing parent relationship for hierarchy
      add :parent_entity_id,
          references(:snmp_physical_entities, type: :binary_id, on_delete: :nilify_all)

      timestamps(type: :utc_datetime)
    end

    create index(:snmp_physical_entities, [:snmp_device_id])
    create unique_index(:snmp_physical_entities, [:snmp_device_id, :entity_index])
    create index(:snmp_physical_entities, [:entity_class])
    create index(:snmp_physical_entities, [:parent_entity_id])
    create index(:snmp_physical_entities, [:serial_number])
  end
end
