defmodule Microwaveprop.Repo.Migrations.CreateContactCommonVolumeRadar do
  use Ecto.Migration

  def change do
    create table(:contact_common_volume_radar, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :contact_id, references(:contacts, type: :binary_id, on_delete: :delete_all),
        null: false

      add :observed_at, :utc_datetime, null: false
      add :common_volume_km2, :float
      add :pixel_count, :integer, null: false, default: 0
      add :rain_pixel_count, :integer, null: false, default: 0
      add :heavy_rain_pixel_count, :integer, null: false, default: 0
      add :core_pixel_count, :integer, null: false, default: 0
      add :max_dbz, :float
      add :mean_dbz, :float
      add :coverage_pct, :float

      timestamps(type: :utc_datetime)
    end

    create unique_index(:contact_common_volume_radar, [:contact_id])
    create index(:contact_common_volume_radar, [:observed_at])

    alter table(:contacts) do
      add :radar_status, :string, null: false, default: "pending"
    end

    create index(:contacts, [:radar_status],
             where: "radar_status::text <> 'complete'::text",
             name: :contacts_radar_status_pending_index
           )
  end
end
