<?php

namespace App\Services\Activity;

use App\Enums\ActivityTypeEnum;
use App\Models\ActivityLog;
use Illuminate\Support\Carbon;

class ActivitySummary
{
    /**
     * Counts per type since $since, including types with no rows.
     *
     * @return array<string, int>
     */
    public function since(Carbon $since): array
    {
        /** @var array<string, int> $counts */
        $counts = ActivityLog::query()
            ->since($since)
            ->selectRaw('type, COUNT(*) as aggregate')
            ->groupBy('type')
            ->pluck('aggregate', 'type')
            ->all();

        $summary = [];

        foreach (ActivityTypeEnum::cases() as $case) {
            $summary[$case->value] = (int) ($counts[$case->value] ?? 0);
        }

        return $summary;
    }
}
