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

  import Mox

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

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

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

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

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

      assert power
      assert power.sensor_descr == "Tx Power"
      assert power.sensor_divisor == 10
    end

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

      assert rssi
      assert rssi.sensor_descr == "Rx Power"
    end

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

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

    test "includes frequency sensor" do
      defs = Siae.wireless_oid_defs()
      freq = Enum.find(defs, &(&1.sensor_type == "frequency"))

      assert freq
      # SIAE frequency in kHz
      assert freq.sensor_divisor == 1000
    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, "3373.1103.80.1.1.11.1") -> {:ok, 180}
          String.contains?(oid, "3373.1103.80.1.1.12.1") -> {:ok, -450}
          String.contains?(oid, "3373.1103.80.1.1.14.1") -> {:ok, 250}
          String.contains?(oid, "3373.1103.80.1.1.3.1") -> {:ok, 71_000_000}
          true -> {:error, :no_such_object}
        end
      end)

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

      assert sensors == []
    end
  end
end
