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

  @disable_ddl_transaction true
  @disable_migration_lock true

  # Mirror of hrrr_profiles for HRDPS-derived rows. Separate table (not a
  # unified nwp_profiles) so HRDPS can be dropped/reprocessed without
  # disturbing the 4500+ HRRR profiles already in prod. Same column set
  # and indexes; PARTITION BY RANGE (valid_time) with quarterly partitions
  # starting from the deploy quarter.
  #
  # Unlike hrrr_profiles, there's no historical archive to backfill into
  # — HRDPS is forward-only — so the partition list starts at the deploy
  # quarter and walks forward.
  def up do
    execute """
    CREATE TABLE hrdps_profiles (
      id uuid NOT NULL DEFAULT gen_random_uuid(),
      valid_time timestamp(0) WITHOUT TIME ZONE NOT NULL,
      lat double precision NOT NULL,
      lon double precision NOT NULL,
      run_time timestamp(0) WITHOUT TIME ZONE,
      profile jsonb[] DEFAULT ARRAY[]::jsonb[],
      hpbl_m double precision,
      pwat_mm double precision,
      surface_temp_c double precision,
      surface_dewpoint_c double precision,
      surface_pressure_mb double precision,
      surface_refractivity double precision,
      min_refractivity_gradient double precision,
      ducting_detected boolean DEFAULT false,
      duct_characteristics jsonb[],
      is_grid_point boolean DEFAULT false,
      inserted_at timestamp(0) WITHOUT TIME ZONE NOT NULL,
      updated_at timestamp(0) WITHOUT TIME ZONE NOT NULL
    ) PARTITION BY RANGE (valid_time)
    """

    partitions = [
      {"2026-04-01", "2026-07-01"},
      {"2026-07-01", "2026-10-01"},
      {"2026-10-01", "2027-01-01"},
      {"2027-01-01", "2027-04-01"},
      {"2027-04-01", "2027-07-01"},
      {"2027-07-01", "2027-10-01"},
      {"2027-10-01", "2028-01-01"}
    ]

    for {from_date, to_date} <- partitions do
      suffix = from_date |> String.replace("-", "_") |> String.slice(0..6)

      execute """
      CREATE TABLE hrdps_profiles_#{suffix} PARTITION OF hrdps_profiles
        FOR VALUES FROM ('#{from_date}') TO ('#{to_date}')
      """
    end

    execute "CREATE UNIQUE INDEX hrdps_profiles_lat_lon_valid_time_index ON hrdps_profiles (lat, lon, valid_time)"
    execute "CREATE INDEX hrdps_profiles_valid_time_index ON hrdps_profiles (valid_time)"
  end

  def down do
    execute "DROP TABLE IF EXISTS hrdps_profiles CASCADE"
  end
end
