<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Sourcing\Aggregate;

final class AggregateRepositoryStatistics
{
    public static function from(array $aggregates): array
    {
        $types = [];
        $events = 0;
        $maxVersion = 0;

        foreach ($aggregates as $aggregate) {
            $type = (string) ($aggregate['aggregateType'] ?? '');
            if ($type !== '') {
                $types[$type] = true;
            }

            $events += count((array) ($aggregate['events'] ?? []));
            $maxVersion = max($maxVersion, (int) ($aggregate['version'] ?? 0));
        }

        return [
            'aggregates' => count($aggregates),
            'aggregateTypes' => count($types),
            'events' => $events,
            'maxVersion' => $maxVersion,
        ];
    }
}
