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

  import Ecto.Query

  def up do
    # Create MikroTik profile
    profile_id = Ecto.UUID.dump!(Ecto.UUID.generate())

    repo().insert_all("device_profiles", [
      %{
        id: profile_id,
        name: "mikrotik",
        vendor: "MikroTik",
        detection_pattern: "RouterOS",
        detection_oid: ".1.3.6.1.4.1.14988",
        priority: 10,
        enabled: true,
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      }
    ])

    # Device identification OIDs using MIB names
    device_oids = [
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        field: "serial_number",
        mib_name: "MIKROTIK-MIB::mtxrSerialNumber.0",
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        field: "firmware_version",
        mib_name: "MIKROTIK-MIB::mtxrFirmwareVersion.0",
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        field: "license_version",
        mib_name: "MIKROTIK-MIB::mtxrLicenseVersion.0",
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        field: "board_name",
        mib_name: "MIKROTIK-MIB::mtxrBoardName.0",
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      }
    ]

    repo().insert_all("profile_device_oids", device_oids)

    # Sensor OIDs using MIB names
    sensor_oids = [
      # Temperatures
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        sensor_type: "temperature",
        mib_name: "MIKROTIK-MIB::mtxrHlTemperature.0",
        sensor_descr: "System Temperature",
        sensor_unit: "°C",
        sensor_divisor: 10,
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        sensor_type: "cpu_temperature",
        mib_name: "MIKROTIK-MIB::mtxrHlCpuTemperature.0",
        sensor_descr: "CPU Temperature",
        sensor_unit: "°C",
        sensor_divisor: 10,
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        sensor_type: "board_temperature",
        mib_name: "MIKROTIK-MIB::mtxrHlBoardTemperature.0",
        sensor_descr: "Board Temperature",
        sensor_unit: "°C",
        sensor_divisor: 10,
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      # Voltages
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        sensor_type: "voltage",
        mib_name: "MIKROTIK-MIB::mtxrHlVoltage.0",
        sensor_descr: "Input Voltage",
        sensor_unit: "V",
        sensor_divisor: 10,
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      # Power & Current
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        sensor_type: "current",
        mib_name: "MIKROTIK-MIB::mtxrHlCurrent.0",
        sensor_descr: "Current",
        sensor_unit: "mA",
        sensor_divisor: 1,
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      },
      %{
        id: Ecto.UUID.dump!(Ecto.UUID.generate()),
        profile_id: profile_id,
        sensor_type: "power",
        mib_name: "MIKROTIK-MIB::mtxrHlPower.0",
        sensor_descr: "Power Consumption",
        sensor_unit: "W",
        sensor_divisor: 10,
        inserted_at: DateTime.utc_now() |> DateTime.truncate(:second),
        updated_at: DateTime.utc_now() |> DateTime.truncate(:second)
      }
    ]

    repo().insert_all("profile_sensor_oids", sensor_oids)
  end

  def down do
    repo().delete_all(from(p in "device_profiles", where: p.name == "mikrotik"))
  end
end
