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

  def change do
    create table(:alerts, 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 :alert_type, :string, null: false
      add :triggered_at, :utc_datetime, null: false
      add :acknowledged_at, :utc_datetime
      add :acknowledged_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :resolved_at, :utc_datetime
      add :message, :text

      timestamps(type: :utc_datetime)
    end

    create index(:alerts, [:equipment_id])
    create index(:alerts, [:triggered_at])
    create index(:alerts, [:alert_type])
    create index(:alerts, [:acknowledged_at])
    create index(:alerts, [:resolved_at])
  end
end
