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

  def up do
    # Drop and recreate the table with correct primary key type
    drop table(:monitoring_checks)

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

      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

    # Recreate indexes
    create index(:monitoring_checks, [:equipment_id])
    create index(:monitoring_checks, [:equipment_id, :checked_at])
    create index(:monitoring_checks, [:checked_at])
  end

  def down do
    # Recreate with old integer primary key
    drop table(:monitoring_checks)

    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 index(:monitoring_checks, [:equipment_id])
    create index(:monitoring_checks, [:equipment_id, :checked_at])
    create index(:monitoring_checks, [:checked_at])
  end
end
