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

  import Mox

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

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

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

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

    test "includes error-ratio sensors" do
      defs = Mimosa.wireless_oid_defs()
      error_sensors = Enum.filter(defs, &(&1.sensor_type == "error-ratio"))

      assert length(error_sensors) == 2
    end

    test "includes frequency sensors" do
      defs = Mimosa.wireless_oid_defs()
      freq_sensors = Enum.filter(defs, &(&1.sensor_type == "frequency"))

      assert length(freq_sensors) == 2
    end

    test "includes power sensors with correct divisor" do
      defs = Mimosa.wireless_oid_defs()
      power_sensors = Enum.filter(defs, &(&1.sensor_type == "power"))

      assert length(power_sensors) >= 4
      # Mimosa power values are divided by 10
      assert Enum.all?(power_sensors, &(&1.sensor_divisor == 10))
    end

    test "includes SNR sensors" do
      defs = Mimosa.wireless_oid_defs()
      snr_sensors = Enum.filter(defs, &(&1.sensor_type == "snr"))

      assert length(snr_sensors) == 2
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 14, fn _, oid, _ ->
        cond do
          String.contains?(oid, "43356.2.1.2.7.3.0") -> {:ok, {:integer, 50}}
          String.contains?(oid, "43356.2.1.2.7.4.0") -> {:ok, {:integer, 30}}
          String.contains?(oid, "43356.2.1.2.6.1.1.6.1") -> {:ok, {:integer, 5800}}
          String.contains?(oid, "43356.2.1.2.6.1.1.6.2") -> {:ok, {:integer, 5805}}
          String.contains?(oid, "43356.2.1.2.6.1.1.2.1") -> {:ok, {:integer, 200}}
          String.contains?(oid, "43356.2.1.2.6.1.1.2.2") -> {:ok, {:integer, 200}}
          String.contains?(oid, "43356.2.1.2.6.1.1.3.1") -> {:ok, {:integer, -500}}
          String.contains?(oid, "43356.2.1.2.6.1.1.3.2") -> {:ok, {:integer, -510}}
          String.contains?(oid, "43356.2.1.2.6.1.1.4.1") -> {:ok, {:integer, -950}}
          String.contains?(oid, "43356.2.1.2.6.1.1.4.2") -> {:ok, {:integer, -960}}
          String.contains?(oid, "43356.2.1.2.6.1.1.5.1") -> {:ok, {:integer, 250}}
          String.contains?(oid, "43356.2.1.2.6.1.1.5.2") -> {:ok, {:integer, 240}}
          String.contains?(oid, "43356.2.1.2.6.2.1.2.1") -> {:ok, {:integer, 800}}
          String.contains?(oid, "43356.2.1.2.6.2.1.5.1") -> {:ok, {:integer, 750}}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Mimosa.discover_wireless_sensors(@client_opts)

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

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

      sensors = Mimosa.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
