BEGIN;

DROP INDEX IF EXISTS idx_fpc_unique;

CREATE UNIQUE INDEX IF NOT EXISTS idx_fpc_unique_non_player_prop
  ON rm_forecast_precomputed (
    date_et,
    event_id,
    forecast_type,
    COALESCE(team_id, ''),
    COALESCE(player_name, '')
  )
  WHERE forecast_type <> 'PLAYER_PROP';

CREATE UNIQUE INDEX IF NOT EXISTS idx_fpc_unique_player_prop
  ON rm_forecast_precomputed (
    date_et,
    event_id,
    forecast_type,
    COALESCE(team_id, ''),
    COALESCE(player_name, ''),
    COALESCE((forecast_payload->>'stat_type'), (forecast_payload->>'normalized_stat_type'), ''),
    COALESCE((forecast_payload->>'market_line_value'), (forecast_payload->>'line'), '')
  )
  WHERE forecast_type = 'PLAYER_PROP';

COMMIT;
