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

  import Mox

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

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

      assert Aerohive.detect_hardware(@client_opts) == "AP650"
    end

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

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

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

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

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

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

    test "includes band-specific client sensors" do
      defs = Aerohive.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_2g.sensor_type == "clients"
      assert clients_5g
      assert clients_5g.sensor_type == "clients"
    end

    test "includes CPU utilization sensor" do
      defs = Aerohive.wireless_oid_defs()
      cpu = Enum.find(defs, &(&1.sensor_descr == "CPU Utilization"))

      assert cpu
      assert cpu.sensor_type == "load"
      assert cpu.sensor_unit == "%"
    end

    test "includes memory utilization sensor" do
      defs = Aerohive.wireless_oid_defs()
      mem = Enum.find(defs, &(&1.sensor_descr == "Memory Utilization"))

      assert mem
      assert mem.sensor_type == "load"
      assert mem.sensor_unit == "%"
    end

    test "includes temperature sensor" do
      defs = Aerohive.wireless_oid_defs()
      temp = Enum.find(defs, &(&1.sensor_type == "temperature"))

      assert temp
      assert temp.sensor_descr == "System Temperature"
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 6, fn _, oid, _ ->
        cond do
          String.contains?(oid, "26928.1.1.1.2.1.1.0") -> {:ok, 45}
          String.contains?(oid, "26928.1.1.1.2.1.2.0") -> {:ok, 20}
          String.contains?(oid, "26928.1.1.1.2.1.3.0") -> {:ok, 25}
          String.contains?(oid, "26928.1.1.1.3.1.0") -> {:ok, 35}
          String.contains?(oid, "26928.1.1.1.3.2.0") -> {:ok, 68}
          String.contains?(oid, "26928.1.1.1.3.3.0") -> {:ok, 42}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Aerohive.discover_wireless_sensors(@client_opts)

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

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

      sensors = Aerohive.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
