defmodule Towerops.Snmp.Profiles.Vendors.Apc do
  @moduledoc """
  APC (Schneider Electric) device-specific SNMP handling.

  Supports APC UPS units and PDUs.

  Supported devices:
  - Smart-UPS series
  - Symmetra UPS
  - Back-UPS Pro
  - Switched/Metered Rack PDU
  - Environmental monitoring cards

  Enterprise OID: 1.3.6.1.4.1.318
  """

  @behaviour Towerops.Snmp.Profiles.Vendors.Vendor

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

  # APC model identifier
  @model_oid "1.3.6.1.4.1.318.1.1.1.1.1.1.0"

  @impl true
  def profile_names, do: ["aos-emu2", "apc", "apc-cpdu", "apc-epdu", "apc-mgeups", "apc-netbotz"]

  @impl true
  def detect_hardware(client_opts) do
    case Client.get(client_opts, @model_oid) do
      {:ok, model} when is_binary(model) -> model
      _ -> nil
    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
    [
      # Battery Capacity %
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.2.2.1.0",
        sensor_type: "load",
        sensor_descr: "Battery Capacity",
        sensor_unit: "%",
        sensor_divisor: 1
      },
      # Battery Temperature (Celsius)
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.2.2.2.0",
        sensor_type: "temperature",
        sensor_descr: "Battery Temperature",
        sensor_unit: "°C",
        sensor_divisor: 1
      },
      # Runtime Remaining (timeticks - needs conversion)
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.2.2.3.0",
        sensor_type: "runtime",
        sensor_descr: "Runtime Remaining",
        sensor_unit: "min",
        sensor_divisor: 6000
      },
      # Output Voltage
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.4.2.1.0",
        sensor_type: "voltage",
        sensor_descr: "Output Voltage",
        sensor_unit: "V",
        sensor_divisor: 1
      },
      # Output Frequency
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.4.2.2.0",
        sensor_type: "frequency",
        sensor_descr: "Output Frequency",
        sensor_unit: "Hz",
        sensor_divisor: 1
      },
      # Output Load %
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.4.2.3.0",
        sensor_type: "load",
        sensor_descr: "Output Load",
        sensor_unit: "%",
        sensor_divisor: 1
      },
      # Output Current
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.4.2.4.0",
        sensor_type: "current",
        sensor_descr: "Output Current",
        sensor_unit: "A",
        sensor_divisor: 1
      },
      # Input Voltage
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.3.2.1.0",
        sensor_type: "voltage",
        sensor_descr: "Input Voltage",
        sensor_unit: "V",
        sensor_divisor: 1
      },
      # Input Frequency
      %{
        oid: "1.3.6.1.4.1.318.1.1.1.3.2.4.0",
        sensor_type: "frequency",
        sensor_descr: "Input Frequency",
        sensor_unit: "Hz",
        sensor_divisor: 1
      }
    ]
  end
end
