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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Raisecom
  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 expected profile names" do
      assert Raisecom.profile_names() == ["raisecom", "raisecom-ros"]
    end
  end

  describe "detect_hardware/1" do
    test "returns hardware model from RAISECOM-SYSTEM-MIB when available" do
      stub(SnmpMock, :get, fn _, oid, _ ->
        case oid do
          "1.3.6.1.4.1.8886.6.1.1.1.2.0" ->
            {:ok, "RAISECOM ISCOM2128EA-MA"}

          _ ->
            {:error, :no_such_object}
        end
      end)

      assert Raisecom.detect_hardware(@client_opts) == "RAISECOM ISCOM2128EA-MA"
    end

    test "falls back to sysDescr when RAISECOM-SYSTEM-MIB is unavailable" do
      stub(SnmpMock, :get, fn _, oid, _ ->
        case oid do
          "1.3.6.1.4.1.8886.6.1.1.1.2.0" ->
            {:error, :no_such_object}

          "1.3.6.1.2.1.1.1.0" ->
            {:ok, "Raisecom Switch Model XYZ"}

          _ ->
            {:error, :no_such_object}
        end
      end)

      assert Raisecom.detect_hardware(@client_opts) == "Raisecom Switch Model XYZ"
    end

    test "returns nil when all hardware detection methods fail" do
      stub(SnmpMock, :get, fn _, _, _ ->
        {:error, :timeout}
      end)

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

    test "truncates long hardware descriptions to 64 characters" do
      long_hardware = String.duplicate("A", 100)

      stub(SnmpMock, :get, fn _, oid, _ ->
        case oid do
          "1.3.6.1.4.1.8886.6.1.1.1.2.0" ->
            {:ok, long_hardware}

          _ ->
            {:error, :no_such_object}
        end
      end)

      result = Raisecom.detect_hardware(@client_opts)
      assert String.length(result) == 64
    end

    test "handles empty hardware string by falling back to sysDescr" do
      stub(SnmpMock, :get, fn _, oid, _ ->
        case oid do
          "1.3.6.1.4.1.8886.6.1.1.1.2.0" ->
            {:ok, ""}

          "1.3.6.1.2.1.1.1.0" ->
            {:ok, "Fallback Description"}

          _ ->
            {:error, :no_such_object}
        end
      end)

      assert Raisecom.detect_hardware(@client_opts) == "Fallback Description"
    end
  end

  describe "wireless_oid_defs/0" do
    test "returns empty list (all sensors defined in YAML)" do
      assert Raisecom.wireless_oid_defs() == []
    end
  end

  describe "discover_wireless_sensors/1" do
    test "returns empty list when no static sensors defined" do
      sensors = Raisecom.discover_wireless_sensors(@client_opts)
      assert sensors == []
    end
  end
end
