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

  def up do
    # Note: TimescaleDB features removed due to Apache license restrictions
    # Using standard PostgreSQL table instead
    create table(:monitoring_checks) do
      add :equipment_id, references(:equipment, type: :binary_id, on_delete: :delete_all),
        null: false

      add :status, :string, null: false
      add :response_time_ms, :integer
      add :checked_at, :utc_datetime, null: false

      timestamps(type: :utc_datetime, updated_at: false)
    end

    # Create indexes for query performance
    create index(:monitoring_checks, [:equipment_id])
    create index(:monitoring_checks, [:equipment_id, :checked_at])
    create index(:monitoring_checks, [:checked_at])

    # Note: Retention and compression policies require TimescaleDB commercial license
    # Commented out for Apache-licensed version
    # execute("""
    # SELECT add_retention_policy('monitoring_checks', INTERVAL '90 days')
    # """)
    #
    # execute("""
    # ALTER TABLE monitoring_checks SET (
    #   timescaledb.compress,
    #   timescaledb.compress_segmentby = 'equipment_id'
    # )
    # """)
    #
    # execute("""
    # SELECT add_compression_policy('monitoring_checks', INTERVAL '7 days')
    # """)
  end

  def down do
    drop table(:monitoring_checks)
  end
end
