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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.ZyxelWlc
  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 zyxelwlc and zyxelnwa profile names" do
      assert ZyxelWlc.profile_names() == ["zyxelwlc", "zyxelnwa"]
    end
  end

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

      assert ZyxelWlc.detect_hardware(@client_opts) == "NXC5500"
    end

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

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

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

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

    test "includes wireless clients sensor" do
      defs = ZyxelWlc.wireless_oid_defs()
      clients = Enum.find(defs, &(&1.sensor_descr == "Wireless Clients"))

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

    test "includes connected APs sensor" do
      defs = ZyxelWlc.wireless_oid_defs()
      aps = Enum.find(defs, &(&1.sensor_descr == "Connected APs"))

      assert aps
      assert aps.sensor_type == "ap-count"
    end

    test "includes CPU and memory sensors" do
      defs = ZyxelWlc.wireless_oid_defs()
      cpu = Enum.find(defs, &(&1.sensor_descr == "CPU Utilization"))
      mem = Enum.find(defs, &(&1.sensor_descr == "Memory Utilization"))

      assert cpu
      assert cpu.sensor_type == "load"
      assert mem
      assert mem.sensor_type == "load"
    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, "890.1.6.22.1.3.1.0") -> {:ok, 250}
          String.contains?(oid, "890.1.6.22.1.3.2.0") -> {:ok, 12}
          String.contains?(oid, "890.1.6.22.1.3.3.0") -> {:ok, 180}
          String.contains?(oid, "890.1.6.22.1.4.1.0") -> {:ok, 35}
          String.contains?(oid, "890.1.6.22.1.4.2.0") -> {:ok, 62}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = ZyxelWlc.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 = ZyxelWlc.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
