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

  def up do
    # Drop child tables first (due to foreign key constraints)
    drop table(:profile_sensor_states)
    drop table(:profile_sensor_definitions)
    drop table(:profile_detection_rules)
    drop table(:profile_processor_definitions)
    drop table(:profile_mempool_definitions)
    drop table(:profile_os_definitions)

    # Drop parent table last
    drop table(:device_profiles)
  end

  def down do
    # Recreate parent table first
    create table(:device_profiles, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :os, :string, null: false
      add :text, :string, null: false
      add :type, :string
      add :icon, :string
      add :group, :string
      add :mib, :string
      add :snmp_bulk, :boolean, default: true
      add :mib_dir, :string
      add :enabled, :boolean, default: true
      add :priority, :integer, default: 100

      timestamps(type: :utc_datetime)
    end

    create unique_index(:device_profiles, [:os])
    create index(:device_profiles, [:enabled])
    create index(:device_profiles, [:type])
    create index(:device_profiles, [:priority])

    # Recreate child tables
    create table(:profile_detection_rules, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :device_profile_id,
          references(:device_profiles, type: :binary_id, on_delete: :delete_all), null: false

      add :rule_type, :string, null: false
      add :oid, :text
      add :operator, :string
      add :value, :text
      add :pattern, :text
      add :priority, :integer, default: 0

      timestamps(type: :utc_datetime)
    end

    create index(:profile_detection_rules, [:device_profile_id])
    create index(:profile_detection_rules, [:rule_type])

    create table(:profile_sensor_definitions, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :device_profile_id,
          references(:device_profiles, type: :binary_id, on_delete: :delete_all), null: false

      add :sensor_class, :string, null: false
      add :oid, :string
      add :num_oid, :string
      add :index, :string
      add :descr, :string
      add :divisor, :integer, default: 1
      add :multiplier, :integer, default: 1
      add :precision, :integer
      add :sensor_type, :string
      add :unit, :string
      add :low_limit, :float
      add :low_warn_limit, :float
      add :warn_limit, :float
      add :high_limit, :float
      add :skip_value_lt, :float
      add :skip_value_gt, :float
      add :user_func, :string
      add :entPhysicalIndex, :string
      add :group, :string
      add :options, :map

      timestamps(type: :utc_datetime)
    end

    create index(:profile_sensor_definitions, [:device_profile_id])
    create index(:profile_sensor_definitions, [:sensor_class])

    create table(:profile_sensor_states, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :sensor_definition_id,
          references(:profile_sensor_definitions, type: :binary_id, on_delete: :delete_all),
          null: false

      add :value, :integer, null: false
      add :descr, :string, null: false
      add :generic, :integer, null: false
      add :graph, :integer, null: false

      timestamps(type: :utc_datetime)
    end

    create index(:profile_sensor_states, [:sensor_definition_id])

    create table(:profile_processor_definitions, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :device_profile_id,
          references(:device_profiles, type: :binary_id, on_delete: :delete_all), null: false

      add :oid, :string, null: false
      add :num_oid, :string, null: false
      add :index, :string
      add :descr, :string
      add :precision, :integer, default: 1
      add :type, :string
      add :warn_percent, :integer
      add :entPhysicalIndex, :string

      timestamps(type: :utc_datetime)
    end

    create index(:profile_processor_definitions, [:device_profile_id])

    create table(:profile_mempool_definitions, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :device_profile_id,
          references(:device_profiles, type: :binary_id, on_delete: :delete_all), null: false

      add :index, :string
      add :descr, :string
      add :total_oid, :string
      add :used_oid, :string
      add :free_oid, :string
      add :percent_used_oid, :string
      add :precision, :integer, default: 1
      add :type, :string
      add :class, :string
      add :warn_percent, :integer

      timestamps(type: :utc_datetime)
    end

    create index(:profile_mempool_definitions, [:device_profile_id])

    create table(:profile_os_definitions, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :device_profile_id,
          references(:device_profiles, type: :binary_id, on_delete: :delete_all), null: false

      add :field, :string, null: false
      add :oid, :string
      add :regex, :string
      add :template, :text
      add :value, :string

      timestamps(type: :utc_datetime)
    end

    create index(:profile_os_definitions, [:device_profile_id])
    create unique_index(:profile_os_definitions, [:device_profile_id, :field])
  end
end
