-- Add 4-hour rain prediction storage table.
-- Safe to re-run.

CREATE TABLE IF NOT EXISTS predictions_rain_4h (
  ts                    TIMESTAMPTZ NOT NULL,
  generated_at          TIMESTAMPTZ NOT NULL DEFAULT now(),
  site                  TEXT NOT NULL,
  model_name            TEXT NOT NULL,
  model_version         TEXT NOT NULL,
  threshold             DOUBLE PRECISION NOT NULL,
  probability           DOUBLE PRECISION NOT NULL,
  predict_rain          BOOLEAN NOT NULL,

  rain_next_4h_mm_actual DOUBLE PRECISION,
  rain_next_4h_actual    BOOLEAN,
  evaluated_at           TIMESTAMPTZ,

  metadata              JSONB,

  PRIMARY KEY (site, model_name, model_version, ts)
);

SELECT create_hypertable('predictions_rain_4h', 'ts', if_not_exists => TRUE);

CREATE INDEX IF NOT EXISTS idx_predictions_rain_4h_site_ts
  ON predictions_rain_4h(site, ts DESC);

CREATE INDEX IF NOT EXISTS idx_predictions_rain_4h_pending_eval
  ON predictions_rain_4h(site, evaluated_at, ts DESC);
