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

  def change do
    alter table(:alerts) do
      # Add check_id reference (nullable for backward compatibility)
      add :check_id, references(:checks, type: :binary_id, on_delete: :delete_all)

      # Add severity (1=WARNING, 2=CRITICAL)
      add :severity, :integer, default: 2

      # Track notification delivery
      add :notification_sent, :boolean, default: false
      add :notification_sent_at, :utc_datetime

      # Output from the check
      add :output, :text
    end

    # Make device_id nullable since checks can be standalone
    execute "ALTER TABLE alerts ALTER COLUMN device_id DROP NOT NULL", ""

    create index(:alerts, [:check_id])
    create index(:alerts, [:severity])
    create index(:alerts, [:notification_sent])
  end
end
