create or replace function ems.fn_site_effective_prices_slots_range(
  p_site_id int,
  p_from timestamptz,
  p_to timestamptz
)
returns jsonb
language sql
stable
as $fn$
  select coalesce(
    jsonb_agg(u.j order by u.interval_start),
    '[]'::jsonb
  )
  from (
    select
      v.interval_start,
      to_jsonb(v) as j
    from ems.vw_site_effective_price v
    where v.site_id = p_site_id
      and v.interval_start >= p_from
      and v.interval_end < p_to
    order by v.interval_start
  ) u;
$fn$;

comment on function ems.fn_site_effective_prices_slots_range(int, timestamptz, timestamptz) is
  'Efektivní ceny pro [p_from, p_to) jako pole JSON řádků view (range pro UI bez PostgREST auth).';

