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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Epmp
  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 expected profile names" do
      assert Epmp.profile_names() == ["epmp"]
    end
  end

  describe "detect_hardware/1" do
    test "returns ePMP AP when interface mode is 1" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.3.8.2.1.0", _ -> {:ok, 1} end)
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.1.1.33.0", _ -> {:ok, 1} end)

      assert Epmp.detect_hardware(@client_opts) == "ePMP AP"
    end

    test "returns ePTP Master when interface mode is 1 and sub mode is 5" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.3.8.2.1.0", _ -> {:ok, 1} end)
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.1.1.33.0", _ -> {:ok, 5} end)

      assert Epmp.detect_hardware(@client_opts) == "ePTP Master"
    end

    test "returns ePMP SM when interface mode is 2" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.3.8.2.1.0", _ -> {:ok, 2} end)
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.1.1.33.0", _ -> {:ok, 1} end)

      assert Epmp.detect_hardware(@client_opts) == "ePMP SM"
    end

    test "returns ePTP Slave when interface mode is 2 and sub mode is 4" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.3.8.2.1.0", _ -> {:ok, 2} end)
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.21.1.1.33.0", _ -> {:ok, 4} end)

      assert Epmp.detect_hardware(@client_opts) == "ePTP Slave"
    end

    test "returns nil when SNMP fails" do
      expect(SnmpMock, :get, fn _, _, _ -> {:error, :timeout} end)

      assert Epmp.detect_hardware(@client_opts) == nil
    end
  end

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

      assert is_list(defs)
      assert length(defs) >= 10
    end

    test "each definition has required fields" do
      defs = Epmp.wireless_oid_defs()

      Enum.each(defs, fn def ->
        assert Map.has_key?(def, :oid)
        assert Map.has_key?(def, :sensor_descr)
        assert Map.has_key?(def, :sensor_type)
        assert Map.has_key?(def, :sensor_unit)
        assert Map.has_key?(def, :sensor_divisor)
      end)
    end

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

      assert rssi
      assert rssi.sensor_type == "rssi"
      assert rssi.sensor_unit == "dBm"
    end

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

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

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      defs = Epmp.wireless_oid_defs()
      num_defs = length(defs)

      expect(SnmpMock, :get, num_defs, fn _, _oid, _ ->
        {:ok, 42}
      end)

      sensors = Epmp.discover_wireless_sensors(@client_opts)

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

    test "returns empty list when no sensors respond" do
      defs = Epmp.wireless_oid_defs()
      num_defs = length(defs)

      expect(SnmpMock, :get, num_defs, fn _, _, _ ->
        {:error, :no_such_object}
      end)

      sensors = Epmp.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
