create or replace function ems.fn_forecast_pv_split(p_site_id int, p_day date)
returns jsonb
language sql
stable
as $fn$
  with tz as (
    select coalesce(nullif(trim(s.timezone), ''), 'Europe/Prague') as tz_name
    from ems.site s
    where s.id = p_site_id
  ),
  bounds as (
    select
      ((p_day::timestamp) at time zone tz.tz_name) as day_start_utc,
      (((p_day + 1)::timestamp) at time zone tz.tz_name) as day_end_utc
    from tz
  ),
  latest as (
    select distinct on (fpi.interval_start, fpr.pv_array_id)
      fpi.run_id,
      fpr.pv_array_id,
      fpi.interval_start,
      fpi.power_w,
      fpi.irradiance_wm2,
      fpi.temp_c,
      apa.code as pv_array_code,
      apa.controllable
    from ems.forecast_pv_interval fpi
    join ems.forecast_pv_run fpr on fpr.id = fpi.run_id
    join ems.asset_pv_array apa
      on apa.id = fpr.pv_array_id
     and apa.site_id = fpr.site_id
    cross join bounds b
    where fpr.site_id = p_site_id
      and fpr.status = 'ok'
      and fpi.interval_start >= b.day_start_utc
      and fpi.interval_start < b.day_end_utc
    order by fpi.interval_start, fpr.pv_array_id, fpr.created_at desc
  ),
  rows as (
    select
      jsonb_build_object(
        'run_id', run_id,
        'pv_array_id', pv_array_id,
        'interval_start', interval_start,
        'power_w', power_w,
        'irradiance_wm2', irradiance_wm2,
        'temp_c', temp_c,
        'pv_array_code', pv_array_code
      ) as j,
      controllable,
      pv_array_code,
      interval_start
    from latest
  )
  select jsonb_build_object(
    'pv_a',
    coalesce(
      (
        select jsonb_agg(j order by pv_array_code, interval_start)
        from rows
        where controllable
      ),
      '[]'::jsonb
    ),
    'pv_b',
    coalesce(
      (
        select jsonb_agg(j order by pv_array_code, interval_start)
        from rows
        where not controllable
      ),
      '[]'::jsonb
    )
  );
$fn$;

comment on function ems.fn_forecast_pv_split(int, date) is
  'Predikce FVE rozsplitěná na pole A (controllable) a B pro UI.';
