/**
 * Count 30-day incidents per country code from a single already-fetched
 * incident list, replacing the per-country serial `statusIncident.count()`
 * N+1 loop in /api/reports/executive.
 *
 * Incident-level tally: each incident contributes exactly ONE to each country
 * it is linked to (a many-to-many relation), regardless of how many monitors
 * it spans — matching the semantics of the old
 * `count({ where: { countries: { some: { id } } } })`.
 */
export function tallyIncidentsByCountry(
    incidents: { countries: { code: string }[] }[],
): Map<string, number> {
    const tally = new Map<string, number>();
    for (const incident of incidents) {
        for (const country of incident.countries) {
            tally.set(country.code, (tally.get(country.code) ?? 0) + 1);
        }
    }
    return tally;
}
