create or replace function ems.fn_modbus_written_command_ids(
  p_site_id int,
  p_lookback interval
)
returns jsonb
language sql
stable
as $fn$
  select coalesce(
    jsonb_agg(mc.id order by mc.written_at),
    '[]'::jsonb
  )
  from ems.modbus_command mc
  where mc.site_id = p_site_id
    and mc.status = 'written'
    and mc.written_at >= now() - p_lookback;
$fn$;

comment on function ems.fn_modbus_written_command_ids(int, interval) is
  'ID written příkazů k ruční verifikaci (GET control/verify).';
