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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Engenius
  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 engenius profile name" do
      assert Engenius.profile_names() == ["engenius"]
    end
  end

  describe "detect_hardware/1" do
    test "detects hardware from model OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.14125.1.1.1.0", _ ->
        {:ok, "EWS377AP"}
      end)

      assert Engenius.detect_hardware(@client_opts) == "EWS377AP"
    end

    test "returns nil when OID unavailable" do
      expect(SnmpMock, :get, fn _, _, _ ->
        {:error, :no_such_object}
      end)

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

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

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

    test "includes total client count sensor" do
      defs = Engenius.wireless_oid_defs()
      clients = Enum.find(defs, &(&1.sensor_descr == "Connected Clients"))

      assert clients
      assert clients.sensor_type == "clients"
    end

    test "includes band-specific client sensors" do
      defs = Engenius.wireless_oid_defs()
      clients_2g = Enum.find(defs, &(&1.sensor_descr == "2.4GHz Clients"))
      clients_5g = Enum.find(defs, &(&1.sensor_descr == "5GHz Clients"))

      assert clients_2g
      assert clients_5g
    end

    test "includes channel utilization sensors" do
      defs = Engenius.wireless_oid_defs()
      util_2g = Enum.find(defs, &(&1.sensor_descr == "2.4GHz Channel Utilization"))
      util_5g = Enum.find(defs, &(&1.sensor_descr == "5GHz Channel Utilization"))

      assert util_2g
      assert util_2g.sensor_type == "load"
      assert util_5g
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 7, fn _, oid, _ ->
        cond do
          String.contains?(oid, "14125.100.1.4.1.0") -> {:ok, 45}
          String.contains?(oid, "14125.100.1.4.2.0") -> {:ok, 20}
          String.contains?(oid, "14125.100.1.4.3.0") -> {:ok, 25}
          String.contains?(oid, "14125.100.1.5.1.0") -> {:ok, 35}
          String.contains?(oid, "14125.100.1.5.2.0") -> {:ok, 68}
          String.contains?(oid, "14125.100.1.6.1.0") -> {:ok, 45}
          String.contains?(oid, "14125.100.1.6.2.0") -> {:ok, 30}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Engenius.discover_wireless_sensors(@client_opts)

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

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

      sensors = Engenius.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
