-- Cache výsledku fn_pv_forecast_delta_profile per site (obnovuje job fn_fill_forecast_accuracy).
-- Zrychlení GET /plan/current a plánování (canonical PV forecast).

alter table ems.site_pv_forecast_calibration
  add column if not exists delta_profile_cache jsonb null,
  add column if not exists delta_profile_cached_at timestamptz null;

comment on column ems.site_pv_forecast_calibration.delta_profile_cache is
  'Poslední JSON z fn_pv_forecast_delta_profile (120d lookback, now); NULL = ještě nepočítáno.';

comment on column ems.site_pv_forecast_calibration.delta_profile_cached_at is
  'Čas posledního refresh cache (fn_refresh_site_pv_delta_profile_cache).';

create index if not exists idx_planning_run_site_comparison_of
  on ems.planning_run (
    site_id,
    ((solver_params->>'comparison_of_run_id')::int),
    created_at desc
  )
  where status = 'comparison';

comment on index ems.idx_planning_run_site_comparison_of is
  'Rychlé nalezení comparison runu pro GET /plan/compare (comparison_of_run_id v solver_params).';
