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

  import Mox

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

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

      assert Pepwave.detect_hardware(@client_opts) == "MAX HD4"
    end

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

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

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

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

    test "includes signal strength sensor" do
      defs = Pepwave.wireless_oid_defs()
      signal = Enum.find(defs, &(&1.sensor_descr == "WAN1 Signal Strength"))

      assert signal
      assert signal.sensor_type == "rssi"
    end

    test "includes CPU and memory sensors" do
      defs = Pepwave.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, 4, fn _, oid, _ ->
        cond do
          String.contains?(oid, "27662.3.1.1.1.0") -> {:ok, -65}
          String.contains?(oid, "27662.3.1.2.1.0") -> {:ok, 1}
          String.contains?(oid, "27662.2.1.1.0") -> {:ok, 25}
          String.contains?(oid, "27662.2.1.2.0") -> {:ok, 60}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Pepwave.discover_wireless_sensors(@client_opts)

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

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

      sensors = Pepwave.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
