defmodule Towerops.Snmp.Profiles.Vendors.ExaltTest do
  use Towerops.DataCase, async: true

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Exalt
  alias Towerops.Snmp.SnmpMock

  setup :verify_on_exit!

  @client_opts [
    ip: "192.168.1.1",
    community: "public",
    version: "2c",
    port: 161,
    timeout: 5000
  ]

  describe "profile_names/0" do
    test "returns exalt profile name" do
      assert Exalt.profile_names() == ["extendair"]
    end
  end

  describe "detect_hardware/1" do
    test "returns nil (hardware detection handled by YAML)" do
      assert Exalt.detect_hardware(@client_opts) == nil
    end
  end

  describe "wireless_oid_defs/0" do
    test "returns list of wireless sensor definitions" do
      defs = Exalt.wireless_oid_defs()

      assert is_list(defs)
      assert [_ | _] = defs
    end

    test "includes frequency sensors with kHz divisor" do
      defs = Exalt.wireless_oid_defs()
      freq_sensors = Enum.filter(defs, &(&1.sensor_type == "frequency"))

      assert length(freq_sensors) == 2
      # Exalt returns frequency in kHz
      assert Enum.all?(freq_sensors, &(&1.sensor_divisor == 1000))
    end

    test "includes power sensor" do
      defs = Exalt.wireless_oid_defs()
      power = Enum.find(defs, &(&1.sensor_type == "power"))

      assert power
      assert power.sensor_descr == "Tx Power"
    end

    test "includes RSSI sensor" do
      defs = Exalt.wireless_oid_defs()
      rssi = Enum.find(defs, &(&1.sensor_type == "rssi"))

      assert rssi
      assert rssi.sensor_descr == "RSL"
    end

    test "includes SNR sensor" do
      defs = Exalt.wireless_oid_defs()
      snr = Enum.find(defs, &(&1.sensor_type == "snr"))

      assert snr
      assert snr.sensor_unit == "dB"
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 5, fn _, oid, _ ->
        cond do
          String.contains?(oid, "25651.1.2.3.1.1.7.0") -> {:ok, 71_000_000}
          String.contains?(oid, "25651.1.2.3.1.1.8.0") -> {:ok, 81_000_000}
          String.contains?(oid, "25651.1.2.3.1.1.9.0") -> {:ok, 200}
          String.contains?(oid, "25651.1.2.3.1.2.1.0") -> {:ok, -550}
          String.contains?(oid, "25651.1.2.3.1.2.3.0") -> {:ok, 280}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Exalt.discover_wireless_sensors(@client_opts)

      assert is_list(sensors)
      assert length(sensors) == 5
    end

    test "returns empty list when no sensors respond" do
      expect(SnmpMock, :get, 5, fn _, _, _ ->
        {:error, :no_such_object}
      end)

      sensors = Exalt.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
