create or replace function ems.fn_site_effective_prices_day_prague(
  p_site_id int,
  p_day date
)
returns jsonb
language sql
stable
as $fn$
  with bounds as (
    select
      ((p_day::timestamp) at time zone 'Europe/Prague') as day_start_utc,
      (((p_day + 1)::timestamp) at time zone 'Europe/Prague') as day_end_utc
  )
  select coalesce(
    jsonb_agg(u.j order by u.interval_start),
    '[]'::jsonb
  )
  from (
    select
      t.interval_start,
      to_jsonb(t) as j
    from (
      select v.*
      from ems.vw_site_effective_price v
      cross join bounds b
      where v.site_id = p_site_id
        and v.interval_start >= b.day_start_utc
        and v.interval_start < b.day_end_utc
      order by v.interval_start
    ) t
  ) u;
$fn$;

comment on function ems.fn_site_effective_prices_day_prague(int, date) is
  'Efektivní ceny pro kalendářní den v TZ Praha jako pole JSON řádků view.';
