defmodule Towerops.Snmp.Profiles.DynamicTest do
  use ExUnit.Case, async: true

  alias Towerops.Snmp.Profiles.Dynamic

  describe "description OID derivation" do
    test "derives correct OID for mtxrGaugeName" do
      value_oid = "1.3.6.1.4.1.14988.1.1.3.100.1.3.17"
      descr = "MIKROTIK-MIB::mtxrGaugeName"

      # Call the private function via module attribute trick
      descr_oid = derive_descr_oid_wrapper(value_oid, descr)

      assert descr_oid == "1.3.6.1.4.1.14988.1.1.3.100.1.1.17"
    end

    test "derives correct OID for mtxrOpticalName" do
      value_oid = "1.3.6.1.4.1.14988.1.1.19.1.1.8.17"
      descr = "MIKROTIK-MIB::mtxrOpticalName"

      descr_oid = derive_descr_oid_wrapper(value_oid, descr)

      assert descr_oid == "1.3.6.1.4.1.14988.1.1.19.1.1.2.17"
    end

    test "derives correct OID for mtxrPOEName" do
      value_oid = "1.3.6.1.4.1.14988.1.1.3.15.1.1.5.3"
      descr = "MIKROTIK-MIB::mtxrPOEName"

      descr_oid = derive_descr_oid_wrapper(value_oid, descr)

      assert descr_oid == "1.3.6.1.4.1.14988.1.1.3.15.1.1.1.3"
    end

    test "handles multiple indices correctly" do
      # Test with different index values
      value_oid_1 = "1.3.6.1.4.1.14988.1.1.3.100.1.3.7101"
      value_oid_2 = "1.3.6.1.4.1.14988.1.1.3.100.1.3.7202"
      descr = "MIKROTIK-MIB::mtxrGaugeName"

      descr_oid_1 = derive_descr_oid_wrapper(value_oid_1, descr)
      descr_oid_2 = derive_descr_oid_wrapper(value_oid_2, descr)

      assert descr_oid_1 == "1.3.6.1.4.1.14988.1.1.3.100.1.1.7101"
      assert descr_oid_2 == "1.3.6.1.4.1.14988.1.1.3.100.1.1.7202"
    end

    test "falls back to generic derivation for unknown MIBs" do
      # Generic SNMP table: .1.3.6.1.4.1.12345.1.2.3.5
      # Should try to replace column 3 with column 1
      value_oid = "1.3.6.1.4.1.12345.1.2.3.5"
      descr = "VENDOR-MIB::someName"

      descr_oid = derive_descr_oid_wrapper(value_oid, descr)

      # Generic derivation replaces second-to-last number with 1
      assert descr_oid == "1.3.6.1.4.1.12345.1.2.1.5"
    end

    test "handles OIDs without index (base walk OIDs)" do
      # Base walk OID without index
      value_oid = "1.3.6.1.4.1.14988.1.1.3.100.1.3"
      descr = "MIKROTIK-MIB::mtxrGaugeName"

      descr_oid = derive_descr_oid_wrapper(value_oid, descr)

      # Should replace column 3 with column 1, preserving the end of string
      assert descr_oid == "1.3.6.1.4.1.14988.1.1.3.100.1.1"
    end
  end

  # Helper to call public function (made public for testing)
  defp derive_descr_oid_wrapper(value_oid, descr) do
    Dynamic.derive_descr_oid(value_oid, descr)
  end
end
