create or replace function ems.fn_energy_flows_daily_month(
  p_site_id int,
  p_month_start date,
  p_month_end date
)
returns jsonb
language sql
stable
as $fn$
  select jsonb_build_object(
    'days',
    coalesce(
      jsonb_agg(t.row_json order by t.day_local),
      '[]'::jsonb
    )
  )
  from (
    select
      (date_trunc('day', ai.interval_start at time zone 'Europe/Prague'))::date as day_local,
      jsonb_build_object(
        'day', (date_trunc('day', ai.interval_start at time zone 'Europe/Prague'))::date,
        'interval_count', count(*)::int,
        'pv_production_kwh', round(sum(coalesce(ai.actual_pv_production_wh, 0)) / 1000, 3),
        'grid_import_kwh', round(sum(ems.fn_audit_grid_import_wh_for_economics(
          ai.actual_grid_import_wh, ai.actual_grid_export_wh, ai.actual_grid_power_w)) / 1000, 3),
        'grid_export_kwh', round(sum(ems.fn_audit_grid_export_wh_for_economics(
          ai.actual_grid_import_wh, ai.actual_grid_export_wh, ai.actual_grid_power_w)) / 1000, 3),
        'batt_charge_kwh', round(sum(coalesce(ai.actual_batt_charge_wh, 0)) / 1000, 3),
        'batt_discharge_kwh', round(sum(coalesce(ai.actual_batt_discharge_wh, 0)) / 1000, 3),
        'load_kwh', round(sum(coalesce(ai.actual_load_consumption_wh, 0)) / 1000, 3),
        'pv_to_load_kwh', round(sum(coalesce(ai.flow_pv_to_load_wh, 0)) / 1000, 3),
        'pv_to_batt_kwh', round(sum(coalesce(ai.flow_pv_to_batt_wh, 0)) / 1000, 3),
        'pv_to_grid_kwh', round(sum(coalesce(ai.flow_pv_to_grid_wh, 0)) / 1000, 3),
        'batt_to_load_kwh', round(sum(coalesce(ai.flow_batt_to_load_wh, 0)) / 1000, 3),
        'batt_to_grid_kwh', round(sum(coalesce(ai.flow_batt_to_grid_wh, 0)) / 1000, 3),
        'grid_to_load_kwh', round(sum(coalesce(ai.flow_grid_to_load_wh, 0)) / 1000, 3),
        'grid_to_batt_kwh', round(sum(coalesce(ai.flow_grid_to_batt_wh, 0)) / 1000, 3),
        'grid_import_cashflow_czk',
        round(
          sum(
            ems.fn_audit_grid_import_wh_for_economics(
              ai.actual_grid_import_wh, ai.actual_grid_export_wh, ai.actual_grid_power_w
            ) / 1000.0
            * coalesce(ep.effective_buy_price_czk_kwh, 0)
          ),
          2
        ),
        'grid_export_revenue_czk',
        round(
          sum(
            ems.fn_audit_grid_export_wh_for_economics(
              ai.actual_grid_import_wh, ai.actual_grid_export_wh, ai.actual_grid_power_w
            ) / 1000.0
            * coalesce(ep.effective_sell_price_czk_kwh, 0)
          ),
          2
        ),
        'grid_to_load_cost_czk',
        round(
          sum(
            coalesce(ai.flow_grid_to_load_wh, 0) / 1000.0
            * coalesce(ep.effective_buy_price_czk_kwh, 0)
          ),
          2
        ),
        'grid_to_batt_cost_czk',
        round(
          sum(
            coalesce(ai.flow_grid_to_batt_wh, 0) / 1000.0
            * coalesce(ep.effective_buy_price_czk_kwh, 0)
          ),
          2
        )
      ) as row_json
    from ems.audit_interval ai
    left join ems.vw_site_effective_price ep
      on ep.site_id = ai.site_id
     and ep.interval_start = ai.interval_start
    where ai.site_id = p_site_id
      and (date_trunc('day', ai.interval_start at time zone 'Europe/Prague'))::date >= p_month_start
      and (date_trunc('day', ai.interval_start at time zone 'Europe/Prague'))::date < p_month_end
    group by 1
    order by 1
  ) t;
$fn$;

comment on function ems.fn_energy_flows_daily_month(int, date, date) is
  'Denní agregace energy flows za měsíc jako JSON pole řádků.';
