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

  Supports Meraki cloud-managed networking devices including:
  - MR series Access Points
  - MS series Switches
  - MX series Security Appliances

  Note: Meraki devices have limited SNMP support compared to traditional
  networking equipment as they are primarily cloud-managed.

  Enterprise OID: 1.3.6.1.4.1.29671
  """

  @behaviour Towerops.Snmp.Profiles.Vendors.Vendor

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

  @impl true
  def profile_names, do: ["merakimr", "merakims", "merakimx"]

  @impl true
  def detect_hardware(client_opts) do
    # Meraki uses sysDescr for hardware identification
    case Client.get(client_opts, "1.3.6.1.2.1.1.1.0") do
      {:ok, descr} when is_binary(descr) ->
        cond do
          String.contains?(descr, "Meraki MR") -> parse_meraki_model(descr, "MR")
          String.contains?(descr, "Meraki CW") -> parse_meraki_model(descr, "CW")
          String.contains?(descr, "Meraki MS") -> parse_meraki_model(descr, "MS")
          String.contains?(descr, "Meraki MX") -> parse_meraki_model(descr, "MX")
          true -> nil
        end

      _ ->
        nil
    end
  end

  defp parse_meraki_model(descr, prefix) do
    # Extract model like "MR46" from sysDescr
    case Regex.run(~r/Meraki (#{prefix}\w+)/, descr) do
      [_, model] -> model
      _ -> "#{prefix} Series"
    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
    # Meraki has limited SNMP support - primarily uses IEEE802dot11-MIB
    # for basic wireless stats when SNMP is enabled
    [
      # Radio Channel (from IEEE802dot11-MIB)
      %{
        oid: "1.2.840.10036.4.5.1.1.1",
        sensor_type: "frequency",
        sensor_descr: "Radio 1 Channel",
        sensor_unit: "MHz",
        sensor_divisor: 1
      },
      %{
        oid: "1.2.840.10036.4.5.1.1.2",
        sensor_type: "frequency",
        sensor_descr: "Radio 2 Channel",
        sensor_unit: "MHz",
        sensor_divisor: 1
      }
    ]
  end
end
