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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Cnpilot
  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 cnpilot profile names" do
      assert Cnpilot.profile_names() == ["cnpilot", "cnpilote", "cnpilotr"]
    end
  end

  describe "detect_hardware/1" do
    test "returns nil (hardware detection handled by YAML)" do
      assert Cnpilot.detect_hardware(@client_opts) == nil
    end
  end

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

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

    test "includes clients sensor" do
      defs = Cnpilot.wireless_oid_defs()
      clients = Enum.find(defs, &(&1.sensor_type == "clients"))

      assert clients
      assert clients.sensor_descr == "Clients"
    end

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

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

    test "includes power sensor" do
      defs = Cnpilot.wireless_oid_defs()
      power = Enum.find(defs, &(&1.sensor_type == "power"))

      assert power
      assert power.sensor_descr == "Transmit Power"
      assert power.sensor_unit == "dBm"
    end

    test "includes noise floor sensor" do
      defs = Cnpilot.wireless_oid_defs()
      noise = Enum.find(defs, &(&1.sensor_type == "noise-floor"))

      assert noise
      assert noise.sensor_descr == "Radio Noise Floor"
      assert noise.sensor_unit == "dBm"
    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, "17713.22.1.1.1.14.0") -> {:ok, {:integer, 25}}
          String.contains?(oid, "17713.22.1.3.1.11.0") -> {:ok, {:integer, 30}}
          String.contains?(oid, "17713.22.1.2.1.8.0") -> {:ok, {:integer, 20}}
          String.contains?(oid, "17713.22.1.2.1.16.0") -> {:ok, {:integer, -95}}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Cnpilot.discover_wireless_sensors(@client_opts)

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

      # Check clients sensor
      clients = Enum.find(sensors, &(&1.sensor_descr == "Clients"))
      assert clients
      assert clients.last_value == 25.0

      # Check noise floor sensor
      noise = Enum.find(sensors, &(&1.sensor_descr == "Radio Noise Floor"))
      assert noise
      assert noise.last_value == -95.0
    end

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

      sensors = Cnpilot.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
