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

  def change do
    create table(:hrrr_native_profiles, primary_key: false) do
      add :id, :binary_id, primary_key: true, null: false
      add :valid_time, :utc_datetime, null: false
      add :run_time, :utc_datetime
      add :lat, :float, null: false
      add :lon, :float, null: false

      # Hybrid-sigma level data, sorted ascending by height (level 1 first).
      # Each array has the same length (HRRR native has 50 hybrid levels);
      # leaving them as arrays avoids a sidecar table and keeps per-profile
      # queries cheap.
      add :level_count, :integer
      add :heights_m, {:array, :float}
      add :temp_k, {:array, :float}
      add :spfh, {:array, :float}
      add :pressure_pa, {:array, :float}
      add :u_wind_ms, {:array, :float}
      add :v_wind_ms, {:array, :float}
      add :tke_m2s2, {:array, :float}

      # Cached surface-level scalars for quick lookup without unpacking arrays.
      add :surface_temp_k, :float
      add :surface_spfh, :float
      add :surface_pressure_pa, :float

      # Derived fields populated by Phase 2/4. Nullable so the schema
      # migration stays additive; the derive task fills them in later.
      add :inversion_top_m, :float
      add :bulk_richardson, :float
      add :theta_e_jump_k, :float
      add :shear_at_top_ms, :float
      add :ducts, :map
      add :best_duct_band_ghz, :float

      timestamps(type: :utc_datetime)
    end

    create unique_index(:hrrr_native_profiles, [:lat, :lon, :valid_time])
    create index(:hrrr_native_profiles, [:valid_time])
  end
end
