defmodule Towerops.Snmp.Profiles.Vendors.Siklu do
  @moduledoc """
  Siklu Communication device-specific SNMP handling.

  Mirrors LibreNMS/OS/Siklu.php for wireless sensor discovery.

  Supported devices:
  - Siklu EtherHaul mmWave radios
  - Siklu MultiHaul series
  """

  @behaviour Towerops.Snmp.Profiles.Vendors.Vendor

  alias Towerops.Snmp.Client
  alias Towerops.Snmp.Profiles.Vendors.Vendor

  # OIDs for hardware detection
  @sw_bank1_running "1.3.6.1.4.1.31926.1.1.1.2.0"
  @sw_bank1_version "1.3.6.1.4.1.31926.1.1.1.3.0"
  @sw_bank2_version "1.3.6.1.4.1.31926.1.1.1.5.0"

  @impl true
  def profile_names, do: ["siklu"]

  @impl true
  def detect_hardware(client_opts) do
    # Siklu hardware is typically detected from sysDescr
    # Version comes from software bank
    case Client.get(client_opts, @sw_bank1_running) do
      {:ok, "running"} ->
        case Client.get(client_opts, @sw_bank1_version) do
          {:ok, version} when is_binary(version) -> version
          _ -> nil
        end

      _ ->
        case Client.get(client_opts, @sw_bank2_version) do
          {:ok, version} when is_binary(version) -> version
          _ -> nil
        end
    end
  end

  @impl true
  def discover_wireless_sensors(client_opts) do
    Vendor.fetch_sensors(wireless_oid_defs(), client_opts)
  end

  @impl true
  def wireless_oid_defs do
    [
      # Operational Frequency (in KHz, divide by 1000 for MHz)
      %{
        oid: "1.3.6.1.4.1.31926.2.1.1.4.1",
        sensor_type: "frequency",
        sensor_descr: "Frequency",
        sensor_unit: "MHz",
        sensor_divisor: 1000
      },
      # Tx Power in dBm
      %{
        oid: "1.3.6.1.4.1.31926.2.1.1.42.1",
        sensor_type: "power",
        sensor_descr: "Tx Power",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # Average RSSI in dBm
      %{
        oid: "1.3.6.1.4.1.31926.2.1.1.19.1",
        sensor_type: "rssi",
        sensor_descr: "RSSI",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # Average CINR (SNR) in dB
      %{
        oid: "1.3.6.1.4.1.31926.2.1.1.18.1",
        sensor_type: "snr",
        sensor_descr: "CINR",
        sensor_unit: "dB",
        sensor_divisor: 1
      }
    ]
  end
end
