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

  def change do
    create table(:weather_stations, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :station_code, :string, null: false
      add :station_type, :string, null: false
      add :wmo_number, :integer
      add :name, :string, null: false
      add :lat, :float, null: false
      add :lon, :float, null: false
      add :elevation_m, :float
      add :state, :string

      timestamps(type: :utc_datetime)
    end

    create unique_index(:weather_stations, [:station_code, :station_type])
    create index(:weather_stations, [:lat, :lon])

    create table(:surface_observations, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :station_id, references(:weather_stations, type: :binary_id), null: false
      add :observed_at, :utc_datetime, null: false
      add :temp_f, :float
      add :dewpoint_f, :float
      add :relative_humidity, :float
      add :wind_speed_kts, :float
      add :wind_direction_deg, :integer
      add :sea_level_pressure_mb, :float
      add :altimeter_setting, :float
      add :sky_condition, :string

      timestamps(type: :utc_datetime)
    end

    create unique_index(:surface_observations, [:station_id, :observed_at])
    create index(:surface_observations, [:observed_at])

    create table(:soundings, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :station_id, references(:weather_stations, type: :binary_id), null: false
      add :observed_at, :utc_datetime, null: false
      add :profile, :map, null: false
      add :level_count, :integer, null: false
      add :surface_pressure_mb, :float
      add :surface_temp_c, :float
      add :surface_dewpoint_c, :float
      add :surface_refractivity, :float
      add :min_refractivity_gradient, :float
      add :boundary_layer_depth_m, :float
      add :precipitable_water_mm, :float
      add :k_index, :float
      add :lifted_index, :float
      add :ducting_detected, :boolean, null: false, default: false
      add :duct_characteristics, :map

      timestamps(type: :utc_datetime)
    end

    create unique_index(:soundings, [:station_id, :observed_at])
    create index(:soundings, [:observed_at])
    create index(:soundings, [:ducting_detected])
  end
end
