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

  Raisecom manufactures networking equipment widely used in Asia-Pacific WISP deployments,
  including switches, routers, and wireless access equipment.

  Sensor discovery is primarily handled via YAML profiles (raisecom.yaml, raisecom-ros.yaml)
  which define processors, memory pools, fan speed, voltage, and state sensors.
  """

  @behaviour Towerops.Snmp.Profiles.Vendors.Vendor

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

  # Raisecom enterprise OID: 1.3.6.1.4.1.8886

  @impl true
  def profile_names, do: ["raisecom", "raisecom-ros"]

  @impl true
  def detect_hardware(client_opts) do
    # Try to get hardware model from RAISECOM-SYSTEM-MIB
    case Client.get(client_opts, "1.3.6.1.4.1.8886.6.1.1.1.2.0") do
      {:ok, hardware} when is_binary(hardware) and byte_size(hardware) > 0 ->
        String.slice(hardware, 0, 64)

      _ ->
        # Fallback to sysDescr
        case Client.get(client_opts, "1.3.6.1.2.1.1.1.0") do
          {:ok, descr} when is_binary(descr) ->
            parse_hardware_from_descr(descr)

          _ ->
            nil
        end
    end
  end

  defp parse_hardware_from_descr(descr) do
    descr
    |> String.split(~r/[\r\n]/)
    |> List.first()
    |> case do
      nil -> nil
      line -> String.slice(line, 0, 64)
    end
  end

  @impl true
  def wireless_oid_defs do
    # All Raisecom sensors (CPU, memory, fans, voltage, state) are defined in YAML profiles
    # No additional wireless-specific sensors needed here
    []
  end

  @impl true
  def discover_wireless_sensors(client_opts) do
    # Fetch any static sensors defined in wireless_oid_defs (currently none)
    Vendor.fetch_sensors(wireless_oid_defs(), client_opts)
  end
end
