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

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

      add :source_device_id, references(:devices, type: :binary_id, on_delete: :delete_all),
        null: false

      add :target_device_id, references(:devices, type: :binary_id, on_delete: :delete_all)

      add :source_interface_id,
          references(:snmp_interfaces, type: :binary_id, on_delete: :nilify_all)

      add :target_interface_id,
          references(:snmp_interfaces, type: :binary_id, on_delete: :nilify_all)

      add :link_type, :string, null: false
      add :confidence, :float, null: false, default: 0.5
      add :metadata, :map, default: %{}
      add :discovered_remote_name, :string
      add :discovered_remote_ip, :string
      add :discovered_remote_mac, :string
      add :last_confirmed_at, :utc_datetime, null: false
      timestamps(type: :utc_datetime)
    end

    create index(:device_links, [:source_device_id])
    create index(:device_links, [:target_device_id])

    create unique_index(
             :device_links,
             [:source_device_id, :source_interface_id, :discovered_remote_mac],
             name: :device_links_src_iface_remote_mac
           )
  end
end
