<?php
declare(strict_types=1);

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

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

        foreach ($snapshots as $snapshot) {
            $aggregateId = (string) ($snapshot['aggregateId'] ?? '');
            $aggregateType = (string) ($snapshot['aggregateType'] ?? '');

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

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

            $maxVersion = max($maxVersion, (int) ($snapshot['version'] ?? 0));
        }

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