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

  import Mox

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

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

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

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

    test "includes distance sensor with km unit" do
      defs = Radwin.wireless_oid_defs()
      distance = Enum.find(defs, &(&1.sensor_type == "distance"))

      assert distance
      assert distance.sensor_unit == "km"
      assert distance.sensor_divisor == 1000
    end

    test "includes power sensors" do
      defs = Radwin.wireless_oid_defs()
      power_sensors = Enum.filter(defs, &(&1.sensor_type == "power"))

      assert length(power_sensors) == 2
    end

    test "includes RSSI sensor" do
      defs = Radwin.wireless_oid_defs()
      rssi = Enum.find(defs, &(&1.sensor_type == "rssi"))

      assert rssi
      assert rssi.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, "4458.1000.1.5.29.0") -> {:ok, {:integer, 5200}}
          String.contains?(oid, "4458.1000.1.5.4.0") -> {:ok, {:integer, 20}}
          String.contains?(oid, "4458.1000.1.5.9.1.0") -> {:ok, {:integer, -50}}
          String.contains?(oid, "4458.1000.1.1.51.7.0") -> {:ok, {:integer, -55}}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Radwin.discover_wireless_sensors(@client_opts)

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

      # Check distance is in km
      distance = Enum.find(sensors, &(&1.sensor_descr == "Link Distance"))
      assert distance
      assert distance.last_value == 5200.0
    end

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

      sensors = Radwin.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
