defmodule Microwaveprop.Commercial.SnmpClientTest do
  use ExUnit.Case, async: true
  use ExUnitProperties

  alias Microwaveprop.Commercial.SnmpClient

  @af11x_output """
  iso.3.6.1.4.1.41112.1.3.2.1.11.1 = INTEGER: -55
  iso.3.6.1.4.1.41112.1.3.2.1.14.1 = INTEGER: -56
  iso.3.6.1.4.1.41112.1.3.1.1.9.1 = INTEGER: 20
  iso.3.6.1.4.1.41112.1.3.2.1.2.1 = INTEGER: 6
  iso.3.6.1.4.1.41112.1.3.2.1.18.1 = INTEGER: 6
  iso.3.6.1.4.1.41112.1.3.2.1.5.1 = INTEGER: 450
  iso.3.6.1.4.1.41112.1.3.2.1.6.1 = INTEGER: 450
  iso.3.6.1.4.1.41112.1.3.2.1.17.1 = INTEGER: 20
  iso.3.6.1.4.1.41112.1.3.2.1.19.1 = INTEGER: -54
  iso.3.6.1.4.1.41112.1.3.2.1.22.1 = INTEGER: -57
  iso.3.6.1.4.1.41112.1.3.2.1.26.1 = INTEGER: 1
  iso.3.6.1.4.1.41112.1.3.2.1.44.1 = INTEGER: 86400
  iso.3.6.1.4.1.41112.1.3.2.1.8.1 = INTEGER: 45
  iso.3.6.1.4.1.41112.1.3.2.1.10.1 = INTEGER: 47
  iso.3.6.1.4.1.41112.1.3.1.1.5.1 = INTEGER: 11245
  iso.3.6.1.4.1.41112.1.3.1.1.6.1 = INTEGER: 10720
  iso.3.6.1.4.1.41112.1.3.2.1.4.1 = INTEGER: 7200
  """

  describe "parse_snmpget_output/2" do
    test "parses AF11X snmpget output into signal map" do
      result = SnmpClient.parse_snmpget_output(@af11x_output, :af11x)

      assert result.rx_power_0 == -55
      assert result.rx_power_1 == -56
      assert result.tx_power == 20
      assert result.cur_tx_mod_rate == 6
      assert result.remote_tx_mod_rate == 6
      assert result.rx_capacity == 450
      assert result.tx_capacity == 450
      assert result.remote_tx_power == 20
      assert result.remote_rx_power_0 == -54
      assert result.remote_rx_power_1 == -57
      assert result.link_state == 1
      assert result.link_uptime == 86_400
      assert result.radio_temp_0_c == 45
      assert result.radio_temp_1_c == 47
      assert result.tx_freq_mhz == 11_245
      assert result.rx_freq_mhz == 10_720
      assert result.link_distance_m == 7200
    end

    test "returns empty map for empty output" do
      assert SnmpClient.parse_snmpget_output("", :af11x) == %{}
    end

    test "skips lines with No Such Instance" do
      output = """
      iso.3.6.1.4.1.41112.1.3.2.1.11.1 = INTEGER: -55
      iso.3.6.1.4.1.41112.1.3.2.1.14.1 = No Such Instance currently exists at this OID
      """

      result = SnmpClient.parse_snmpget_output(output, :af11x)
      assert result.rx_power_0 == -55
      refute Map.has_key?(result, :rx_power_1)
    end
  end

  @af60_static_output """
  iso.3.6.1.4.1.41112.1.11.1.2.5.1 = INTEGER: 52
  iso.3.6.1.4.1.41112.1.11.1.2.6.1 = INTEGER: 1
  iso.3.6.1.4.1.41112.1.11.1.1.2.1 = INTEGER: 60480
  """

  @af60_station_output """
  iso.3.6.1.4.1.41112.1.11.1.3.1.3.170.211.109.42.26.137 = INTEGER: -58
  iso.3.6.1.4.1.41112.1.11.1.3.1.4.170.211.109.42.26.137 = INTEGER: 18
  iso.3.6.1.4.1.41112.1.11.1.3.1.5.170.211.109.42.26.137 = INTEGER: 9
  iso.3.6.1.4.1.41112.1.11.1.3.1.6.170.211.109.42.26.137 = INTEGER: 8
  iso.3.6.1.4.1.41112.1.11.1.3.1.7.170.211.109.42.26.137 = INTEGER: 900000
  iso.3.6.1.4.1.41112.1.11.1.3.1.8.170.211.109.42.26.137 = INTEGER: 850000
  iso.3.6.1.4.1.41112.1.11.1.3.1.15.170.211.109.42.26.137 = INTEGER: 2800
  iso.3.6.1.4.1.41112.1.11.1.3.1.17.170.211.109.42.26.137 = INTEGER: 8640000
  iso.3.6.1.4.1.41112.1.11.1.3.1.18.170.211.109.42.26.137 = INTEGER: -57
  iso.3.6.1.4.1.41112.1.11.1.3.1.19.170.211.109.42.26.137 = INTEGER: 19
  """

  describe "parse_af60_output/2" do
    test "parses AF60 static + station output" do
      result = SnmpClient.parse_af60_output(@af60_static_output, @af60_station_output)

      assert result.radio_temp_0_c == 52
      assert result.link_state == 1
      assert result.tx_freq_mhz == 60_480
      assert result.rx_power_0 == -58
      assert result.tx_power == 18
      assert result.cur_tx_mod_rate == 9
      assert result.remote_tx_mod_rate == 8
      assert result.tx_capacity == 900
      assert result.rx_capacity == 850
      assert result.link_distance_m == 2800
      assert result.link_uptime == 86_400
      assert result.remote_rx_power_0 == -57
      assert result.remote_tx_power == 19
    end

    test "returns empty map for empty output" do
      assert SnmpClient.parse_af60_output("", "") == %{}
    end

    test "converts tx/rx_capacity from kbps to mbps (integer division)" do
      # 450_500 kbps should round down to 450 mbps.
      station = """
      iso.3.6.1.4.1.41112.1.11.1.3.1.7.170.211.109.42.26.137 = INTEGER: 450500
      iso.3.6.1.4.1.41112.1.11.1.3.1.8.170.211.109.42.26.137 = INTEGER: 999
      """

      result = SnmpClient.parse_af60_output("", station)
      assert result.tx_capacity == 450
      # 999 kbps → 0 mbps (floor division)
      assert result.rx_capacity == 0
    end

    test "converts link_uptime from centiseconds to seconds" do
      station = """
      iso.3.6.1.4.1.41112.1.11.1.3.1.17.170.211.109.42.26.137 = INTEGER: 12345
      """

      result = SnmpClient.parse_af60_output("", station)
      assert result.link_uptime == 123
    end

    test "ignores unknown OID columns in the station output" do
      station = """
      iso.3.6.1.4.1.41112.1.11.1.3.1.3.170.211.109.42.26.137 = INTEGER: -42
      iso.3.6.1.4.1.41112.1.11.1.3.1.99.170.211.109.42.26.137 = INTEGER: 7777
      """

      result = SnmpClient.parse_af60_output("", station)
      assert result.rx_power_0 == -42
      refute Map.has_key?(result, :col_99)
      assert map_size(result) == 1
    end
  end

  describe "parse_snmpget_output/2 OID normalisation" do
    test "accepts SNMPv2-SMI::enterprises.* OID prefix" do
      output = """
      SNMPv2-SMI::enterprises.41112.1.3.2.1.11.1 = INTEGER: -61
      """

      result = SnmpClient.parse_snmpget_output(output, :af11x)
      assert result.rx_power_0 == -61
    end

    test "skips lines that don't parse as OID = value" do
      output = """
      iso.3.6.1.4.1.41112.1.3.2.1.11.1 = INTEGER: -55
      garbage line
      """

      result = SnmpClient.parse_snmpget_output(output, :af11x)
      assert result.rx_power_0 == -55
      assert map_size(result) == 1
    end

    test "skips values that aren't parseable as integers" do
      output = """
      iso.3.6.1.4.1.41112.1.3.2.1.11.1 = STRING: "not-a-number"
      iso.3.6.1.4.1.41112.1.3.2.1.14.1 = INTEGER: -56
      """

      result = SnmpClient.parse_snmpget_output(output, :af11x)
      refute Map.has_key?(result, :rx_power_0)
      assert result.rx_power_1 == -56
    end
  end

  describe "poll/3 dispatch" do
    test "unknown radio type raises FunctionClauseError" do
      # There's no catch-all in poll/3 — an unsupported radio_type should
      # fail loudly rather than silently mis-poll.
      assert_raise FunctionClauseError, fn ->
        SnmpClient.poll("10.0.0.1", "public", "unknown-model")
      end
    end
  end

  describe "parse_snmpget_output/2 additional edge cases" do
    test "lines without the ' = ' separator are skipped" do
      # parse_snmp_line needs `OID = TYPE: value`. Any line that doesn't
      # split on ' = ' into two parts is silently dropped.
      output = """
      iso.3.6.1.4.1.41112.1.3.2.1.11.1 = INTEGER: -60
      iso.3.6.1.4.1.41112.1.3.2.1.14.1: INTEGER -55
      """

      result = SnmpClient.parse_snmpget_output(output, :af11x)
      assert result.rx_power_0 == -60
      refute Map.has_key?(result, :rx_power_1)
    end

    test "quoted STRING values are stripped of quotes; non-numeric content is dropped" do
      output = """
      iso.3.6.1.4.1.41112.1.3.2.1.11.1 = STRING: "-58"
      iso.3.6.1.4.1.41112.1.3.2.1.14.1 = STRING: "abc"
      """

      result = SnmpClient.parse_snmpget_output(output, :af11x)
      # Quoted integer survives via Integer.parse's "succeed with trailing".
      assert result.rx_power_0 == -58
      # Non-numeric string doesn't parse → dropped.
      refute Map.has_key?(result, :rx_power_1)
    end

    test "leading-dot OID prefix is normalised before the AF11X lookup" do
      # `normalize_oid` strips a leading `.` and `iso.` so the fully-
      # qualified form with a leading dot still matches.
      output = ".1.3.6.1.4.1.41112.1.3.1.1.9.1 = INTEGER: 19\n"

      result = SnmpClient.parse_snmpget_output(output, :af11x)
      assert result.tx_power == 19
    end
  end

  describe "parse_af60_output/2 additional edge cases" do
    test "static-only input yields a map with just the static fields" do
      static = """
      iso.3.6.1.4.1.41112.1.11.1.2.5.1 = INTEGER: 41
      iso.3.6.1.4.1.41112.1.11.1.2.6.1 = INTEGER: 0
      iso.3.6.1.4.1.41112.1.11.1.1.2.1 = INTEGER: 60160
      """

      result = SnmpClient.parse_af60_output(static, "")
      assert result.radio_temp_0_c == 41
      assert result.link_state == 0
      assert result.tx_freq_mhz == 60_160
      # No station data at all.
      refute Map.has_key?(result, :rx_power_0)
    end

    test "station-only input (no static) still produces the station fields" do
      station = """
      iso.3.6.1.4.1.41112.1.11.1.3.1.3.1.2.3.4.5.6 = INTEGER: -70
      iso.3.6.1.4.1.41112.1.11.1.3.1.4.1.2.3.4.5.6 = INTEGER: 12
      """

      result = SnmpClient.parse_af60_output("", station)
      assert result.rx_power_0 == -70
      assert result.tx_power == 12
      refute Map.has_key?(result, :radio_temp_0_c)
    end
  end

  describe "OID normalisation across prefix variants" do
    test "fully-qualified 1.3.6.1.4.1 OIDs (no `iso.` prefix) parse identically" do
      # Some snmpget configurations emit the fully-qualified dotted form
      # directly instead of using `iso.` — both should resolve the same
      # AF11X field.
      output = "1.3.6.1.4.1.41112.1.3.2.1.11.1 = INTEGER: -62\n"
      result = SnmpClient.parse_snmpget_output(output, :af11x)
      assert result.rx_power_0 == -62
    end

    test "a double-leading-dot OID still strips exactly one leading dot" do
      # `normalize_oid` only peels off ONE leading `.` — the residual
      # leading dot means the lookup against the OID map fails, so the
      # field is silently dropped rather than raising.
      output = "..1.3.6.1.4.1.41112.1.3.2.1.14.1 = INTEGER: -63\n"
      result = SnmpClient.parse_snmpget_output(output, :af11x)
      refute Map.has_key?(result, :rx_power_1)
      assert result == %{}
    end
  end

  describe "poll/3 totality" do
    test "empty radio type string raises FunctionClauseError" do
      assert_raise FunctionClauseError, fn ->
        SnmpClient.poll("10.0.0.1", "public", "")
      end
    end

    test "non-string radio type raises FunctionClauseError" do
      # poll/3 pattern matches on literal binary radio types; any other
      # binary that isn't in the dispatch table should fail loudly so a
      # mis-typed config surfaces rather than silently mis-polling.
      assert_raise FunctionClauseError, fn ->
        SnmpClient.poll("10.0.0.1", "public", "AF11X")
      end
    end
  end

  describe "parse_snmpget_output/2 property" do
    # Pick any AF11X integer value and any recognised AF11X OID suffix, render
    # the canonical `iso.<oid> = INTEGER: <value>` line, and assert the value
    # round-trips through parse_snmpget_output unmodified.
    @af11x_oid_to_field %{
      "1.3.6.1.4.1.41112.1.3.2.1.11.1" => :rx_power_0,
      "1.3.6.1.4.1.41112.1.3.2.1.14.1" => :rx_power_1,
      "1.3.6.1.4.1.41112.1.3.1.1.9.1" => :tx_power,
      "1.3.6.1.4.1.41112.1.3.2.1.26.1" => :link_state,
      "1.3.6.1.4.1.41112.1.3.2.1.44.1" => :link_uptime,
      "1.3.6.1.4.1.41112.1.3.1.1.5.1" => :tx_freq_mhz,
      "1.3.6.1.4.1.41112.1.3.2.1.4.1" => :link_distance_m
    }

    property "parse_snmpget_output never raises on arbitrary inputs (totality)" do
      # Feed the parser whatever text StreamData hands back — newlines,
      # binary data, partial SNMP-looking fragments. The contract is that
      # unparseable content is silently skipped and the result is always
      # a map, never a raised exception.
      check all(garbage <- StreamData.string(:printable, max_length: 500)) do
        result = SnmpClient.parse_snmpget_output(garbage, :af11x)
        assert is_map(result)
      end
    end

    property "rendered snmpget lines round-trip through parse_snmpget_output" do
      oids = Map.keys(@af11x_oid_to_field)

      check all(
              oid <- member_of(oids),
              value <- integer(-2_000_000..2_000_000)
            ) do
        # snmpget's textual form renders `iso.` as the stand-in for `.1`
        # (OID root `.1`, not `.1.3.6.1`), so we render the OID with the
        # leading `1.` stripped and a literal `iso.` prefix. The client's
        # `normalize_oid` puts it back to the dotted numeric form.
        "1." <> tail = oid
        line = "iso.#{tail} = INTEGER: #{value}"
        field = Map.fetch!(@af11x_oid_to_field, oid)
        result = SnmpClient.parse_snmpget_output(line, :af11x)
        assert Map.fetch!(result, field) == value
        # And the OID string we hand-built starts with the UBNT
        # enterprise prefix.
        assert String.starts_with?(oid, "1.3.6.1.4.1.41112")
      end
    end
  end
end
