import type { ElectricMonthGroup } from '../types'; export function summarizeElectricMonths(months: ElectricMonthGroup[] | null) { const source = months ?? []; const totalKwh = source.reduce((sum, month) => sum + (month.kwh || 0), 0); const totalFee = source.reduce((sum, month) => sum + (month.fee || 0), 0); const activeDays = source.reduce( (sum, month) => sum + month.rows.filter(row => row.kwh > 0).length, 0, ); const abnormalDays = source.reduce( (sum, month) => sum + month.rows.filter(row => Math.abs(row.chainPct) >= 0.3).length, 0, ); return { totalKwh, totalFee, activeDays, abnormalDays, avgKwh: activeDays > 0 ? totalKwh / activeDays : 0, avgPrice: totalKwh > 0 ? totalFee / totalKwh : 0, hasFeeDetail: totalFee > 0, }; }