defmodule Towerops.Snmp.Profiles.Vendors.Airfiber do
  @moduledoc """
  Ubiquiti AirFiber device-specific SNMP handling.

  Supports AirFiber 5, 5X, 5XHD, 24, 24HD, 60 series, and LTU series.

  Supported devices:
  - AirFiber 5/5X/5XHD
  - AirFiber 24/24HD
  - AirFiber 60/60 LR
  - AirFiber 60 XG
  - AirFiber LTU (LTU Rocket, LTU-PRO, etc.)
  """

  @behaviour Towerops.Snmp.Profiles.Vendors.Vendor

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

  @impl true
  def profile_names do
    ["airos-af", "airos-af-ltu", "airos-af60"]
  end

  @impl true
  def detect_hardware(_client_opts) do
    # Hardware detection handled by YAML profile
    nil
  end

  @impl true
  def discover_wireless_sensors(client_opts) do
    # Detect if this is an LTU device by checking for LTU-specific OID
    case Client.get(client_opts, "1.3.6.1.4.1.41112.1.10.1.2.2.0") do
      {:ok, _frequency} ->
        # LTU device detected - use AFLTU MIB OIDs
        Vendor.fetch_sensors(ltu_wireless_oid_defs(), client_opts)

      {:error, _} ->
        # Regular AirFiber device - use AirFIBER MIB OIDs
        Vendor.fetch_sensors(airfiber_wireless_oid_defs(), client_opts)
    end
  end

  @impl true
  def wireless_oid_defs do
    # Default to regular AirFiber OIDs for backward compatibility
    airfiber_wireless_oid_defs()
  end

  # UBNT-AirFIBER-MIB OIDs for regular AirFiber devices
  # Enterprise OID: 1.3.6.1.4.1.41112.1.3
  defp airfiber_wireless_oid_defs do
    [
      # TX Frequency (MHz)
      %{
        oid: "1.3.6.1.4.1.41112.1.3.2.1.6.1",
        sensor_type: "frequency",
        sensor_descr: "Tx Frequency",
        sensor_unit: "MHz",
        sensor_divisor: 1
      },
      # RX Frequency (MHz)
      %{
        oid: "1.3.6.1.4.1.41112.1.3.2.1.7.1",
        sensor_type: "frequency",
        sensor_descr: "Rx Frequency",
        sensor_unit: "MHz",
        sensor_divisor: 1
      },
      # TX Power (dBm)
      %{
        oid: "1.3.6.1.4.1.41112.1.3.2.1.8.1",
        sensor_type: "power",
        sensor_descr: "Tx Power",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # RX Power 0 (chain 0)
      %{
        oid: "1.3.6.1.4.1.41112.1.3.2.1.13.1",
        sensor_type: "rssi",
        sensor_descr: "Rx Power Chain 0",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # RX Power 1 (chain 1)
      %{
        oid: "1.3.6.1.4.1.41112.1.3.2.1.14.1",
        sensor_type: "rssi",
        sensor_descr: "Rx Power Chain 1",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # TX Capacity (Mbps)
      %{
        oid: "1.3.6.1.4.1.41112.1.3.2.1.20.1",
        sensor_type: "rate",
        sensor_descr: "Tx Capacity",
        sensor_unit: "Mbps",
        sensor_divisor: 1
      },
      # RX Capacity (Mbps)
      %{
        oid: "1.3.6.1.4.1.41112.1.3.2.1.21.1",
        sensor_type: "rate",
        sensor_descr: "Rx Capacity",
        sensor_unit: "Mbps",
        sensor_divisor: 1
      }
    ]
  end

  # UBNT-AFLTU-MIB OIDs for AirFiber LTU devices
  # Enterprise OID: 1.3.6.1.4.1.41112.1.10
  defp ltu_wireless_oid_defs do
    [
      # Frequency (MHz) - Configuration value
      %{
        oid: "1.3.6.1.4.1.41112.1.10.1.2.2.0",
        sensor_type: "frequency",
        sensor_descr: "Frequency",
        sensor_unit: "MHz",
        sensor_divisor: 1
      },
      # TX Power (dBm EIRP)
      %{
        oid: "1.3.6.1.4.1.41112.1.10.1.2.6.0",
        sensor_type: "power",
        sensor_descr: "Tx EIRP",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # Station RX Power Chain 0 (dBm) - uses station table index 1
      %{
        oid: "1.3.6.1.4.1.41112.1.10.1.4.1.5.1",
        sensor_type: "rssi",
        sensor_descr: "Rx Power Chain 0",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # Station RX Power Chain 1 (dBm) - uses station table index 1
      %{
        oid: "1.3.6.1.4.1.41112.1.10.1.4.1.6.1",
        sensor_type: "rssi",
        sensor_descr: "Rx Power Chain 1",
        sensor_unit: "dBm",
        sensor_divisor: 1
      },
      # Station TX Capacity (Kbps) - convert to Mbps by dividing by 1000
      %{
        oid: "1.3.6.1.4.1.41112.1.10.1.4.1.3.1",
        sensor_type: "rate",
        sensor_descr: "Tx Capacity",
        sensor_unit: "Mbps",
        sensor_divisor: 1000
      },
      # Station RX Capacity (Kbps) - convert to Mbps by dividing by 1000
      %{
        oid: "1.3.6.1.4.1.41112.1.10.1.4.1.4.1",
        sensor_type: "rate",
        sensor_descr: "Rx Capacity",
        sensor_unit: "Mbps",
        sensor_divisor: 1000
      }
    ]
  end
end
