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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Baicells
  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 baicells profile names" do
      profiles = Baicells.profile_names()

      assert "baicells" in profiles
      assert "baicells-od04" in profiles
    end
  end

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

      assert Baicells.detect_hardware(@client_opts) == "Atom OD04"
    end

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

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

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

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

    test "includes RSRP sensor" do
      defs = Baicells.wireless_oid_defs()
      rsrp = Enum.find(defs, &(&1.sensor_descr == "RSRP"))

      assert rsrp
      assert rsrp.sensor_type == "rssi"
      assert rsrp.sensor_unit == "dBm"
    end

    test "includes RSRQ sensor" do
      defs = Baicells.wireless_oid_defs()
      rsrq = Enum.find(defs, &(&1.sensor_descr == "RSRQ"))

      assert rsrq
      assert rsrq.sensor_type == "quality"
      assert rsrq.sensor_unit == "dB"
    end

    test "includes SINR sensor" do
      defs = Baicells.wireless_oid_defs()
      sinr = Enum.find(defs, &(&1.sensor_descr == "SINR"))

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

    test "includes tx power sensor" do
      defs = Baicells.wireless_oid_defs()
      tx = Enum.find(defs, &(&1.sensor_descr == "Tx Power"))

      assert tx
      assert tx.sensor_type == "power"
    end

    test "includes temperature sensor" do
      defs = Baicells.wireless_oid_defs()
      temp = Enum.find(defs, &(&1.sensor_type == "temperature"))

      assert temp
      assert temp.sensor_descr == "Device Temperature"
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 6, fn _, oid, _ ->
        cond do
          String.contains?(oid, "48290.2.1.1.0") -> {:ok, -95}
          String.contains?(oid, "48290.2.1.2.0") -> {:ok, -12}
          String.contains?(oid, "48290.2.1.3.0") -> {:ok, 8}
          String.contains?(oid, "48290.2.1.4.0") -> {:ok, -70}
          String.contains?(oid, "48290.2.1.5.0") -> {:ok, 23}
          String.contains?(oid, "48290.1.2.1.0") -> {:ok, 38}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Baicells.discover_wireless_sensors(@client_opts)

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

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

      sensors = Baicells.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
