create or replace function ems.fn_energy_flows_intervals_day(p_site_id int, p_day date)
returns jsonb
language sql
stable
as $fn$
  select coalesce(
    jsonb_agg(
      jsonb_build_object(
        'interval_start', ai.interval_start,
        'pv_production_kwh',
        case
          when ai.actual_pv_production_wh is null then null
          else round(ai.actual_pv_production_wh::numeric / 1000, 4)
        end,
        'grid_import_kwh',
        case
          when ai.actual_grid_import_wh is null
           and ai.actual_grid_export_wh is null
           and ai.actual_grid_power_w is null
          then null
          else round(
            ems.fn_audit_grid_import_wh_for_economics(
              ai.actual_grid_import_wh, ai.actual_grid_export_wh, ai.actual_grid_power_w
            )::numeric / 1000,
            4
          )
        end,
        'grid_export_kwh',
        case
          when ai.actual_grid_import_wh is null
           and ai.actual_grid_export_wh is null
           and ai.actual_grid_power_w is null
          then null
          else round(
            ems.fn_audit_grid_export_wh_for_economics(
              ai.actual_grid_import_wh, ai.actual_grid_export_wh, ai.actual_grid_power_w
            )::numeric / 1000,
            4
          )
        end,
        'batt_charge_kwh',
        case
          when ai.actual_batt_charge_wh is null then null
          else round(ai.actual_batt_charge_wh::numeric / 1000, 4)
        end,
        'batt_discharge_kwh',
        case
          when ai.actual_batt_discharge_wh is null then null
          else round(ai.actual_batt_discharge_wh::numeric / 1000, 4)
        end,
        'load_kwh',
        case
          when ai.actual_load_consumption_wh is null then null
          else round(ai.actual_load_consumption_wh::numeric / 1000, 4)
        end,
        'pv_to_load_kwh',
        case
          when ai.flow_pv_to_load_wh is null then null
          else round(ai.flow_pv_to_load_wh::numeric / 1000, 4)
        end,
        'pv_to_batt_kwh',
        case
          when ai.flow_pv_to_batt_wh is null then null
          else round(ai.flow_pv_to_batt_wh::numeric / 1000, 4)
        end,
        'pv_to_grid_kwh',
        case
          when ai.flow_pv_to_grid_wh is null then null
          else round(ai.flow_pv_to_grid_wh::numeric / 1000, 4)
        end,
        'batt_to_load_kwh',
        case
          when ai.flow_batt_to_load_wh is null then null
          else round(ai.flow_batt_to_load_wh::numeric / 1000, 4)
        end,
        'batt_to_grid_kwh',
        case
          when ai.flow_batt_to_grid_wh is null then null
          else round(ai.flow_batt_to_grid_wh::numeric / 1000, 4)
        end,
        'grid_to_load_kwh',
        case
          when ai.flow_grid_to_load_wh is null then null
          else round(ai.flow_grid_to_load_wh::numeric / 1000, 4)
        end,
        'grid_to_batt_kwh',
        case
          when ai.flow_grid_to_batt_wh is null then null
          else round(ai.flow_grid_to_batt_wh::numeric / 1000, 4)
        end
      )
      order by ai.interval_start
    ),
    '[]'::jsonb
  )
  from ems.audit_interval ai
  where ai.site_id = p_site_id
    and (date_trunc('day', ai.interval_start at time zone 'Europe/Prague'))::date = p_day;
$fn$;

comment on function ems.fn_energy_flows_intervals_day(int, date) is
  '15min energy flows pro jeden kalendářní den (Prague) jako JSON pole.';
