defmodule Towerops.Repo.Migrations.FixMikrotikTemperatureSensorDivisor do
  use Ecto.Migration

  def up do
    # Update Mikrotik temperature sensors (from gauge table) to use divisor 10
    # Mikrotik reports temperature in tenths of degrees (e.g., 230 = 23.0°C)
    # Match by OID pattern: 1.3.6.1.4.1.14988.1.1.3.100.1.3.* (gauge table)
    execute """
    UPDATE snmp_sensors
    SET sensor_divisor = 10
    WHERE sensor_type = 'temperature'
      AND (
        sensor_index LIKE 'mikrotik_gauge_%'
        OR sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%'
      )
      AND sensor_divisor = 1
    """

    # Note: Optical table sensors (1.3.6.1.4.1.14988.1.1.19.1.1.6.*)
    # correctly use divisor 1 and are not affected by this migration
  end

  def down do
    # Revert to divisor 1 (incorrect, but reversible)
    execute """
    UPDATE snmp_sensors
    SET sensor_divisor = 1
    WHERE sensor_type = 'temperature'
      AND (
        sensor_index LIKE 'mikrotik_gauge_%'
        OR sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%'
      )
      AND sensor_divisor = 10
    """
  end
end
