<?php
declare(strict_types=1);

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

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

        foreach ($events as $event) {
            $aggregateId = (string) ($event['aggregateId'] ?? '');
            $aggregateType = (string) ($event['aggregateType'] ?? '');

            if ($aggregateId !== '') {
                $aggregates[$aggregateId] = true;
            }
            if ($aggregateType !== '') {
                $types[$aggregateType] = true;
            }
        }

        return [
            'events' => count($events),
            'aggregates' => count($aggregates),
            'aggregateTypes' => count($types),
            'lastSequence' => count($events) > 0 ? max(array_map(static fn (array $event): int => (int) ($event['sequence'] ?? 0), $events)) : 0,
        ];
    }
}
