-- statistiky importovaných OTE slotů pro kalendářní den v Europe/Prague

create or replace function ems.fn_ote_day_slot_stats_prague(p_day date)
returns jsonb
language sql
stable
as $fn$
  select jsonb_build_object(
    'count',
    coalesce(
      (
        select count(*)::int
        from ems.market_interval_price mip
        where mip.market_source = 'OTE_CZ'
          and (mip.interval_start at time zone 'Europe/Prague')::date = p_day
      ),
      0
    ),
    'first_price',
    (
      select mip.buy_raw_price_czk_kwh
      from ems.market_interval_price mip
      where mip.market_source = 'OTE_CZ'
        and (mip.interval_start at time zone 'Europe/Prague')::date = p_day
      order by mip.interval_start
      limit 1
    ),
    'is_complete',
    coalesce(
      (
        select count(*)::int
        from ems.market_interval_price mip
        where mip.market_source = 'OTE_CZ'
          and (mip.interval_start at time zone 'Europe/Prague')::date = p_day
      ),
      0
    ) in (92, 96, 100)
  );
$fn$;

comment on function ems.fn_ote_day_slot_stats_prague(date) is
  'Počet slotů OTE_CZ, první cena a zda den vypadá kompletně (92/96/100) v TZ Praha.';
