-- denní součet |battery_power_w|/60 Wh → poměr k 2× usable (orientační cykly/den)

create or replace view ems.vw_battery_cycle_daily as
select
  ti.site_id,
  (ti.measured_at at time zone 'Europe/Prague')::date as day_prague,
  sum(abs(ti.battery_power_w::numeric) / 60.0) as throughput_wh,
  max(ab.usable_wh) as usable_wh,
  round(
    (sum(abs(ti.battery_power_w::numeric) / 60.0)
      / nullif(max(ab.usable_wh) * 2, 0))::numeric,
    4
  ) as equiv_full_cycles
from ems.telemetry_inverter ti
cross join lateral (
  select usable_capacity_wh::numeric as usable_wh
  from ems.asset_battery b
  where b.site_id = ti.site_id
  order by b.id
  limit 1
) ab
where ti.battery_power_w is not null
group by ti.site_id, (ti.measured_at at time zone 'Europe/Prague')::date;
