<?php
declare(strict_types=1);

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

final class ProjectionStatistics
{
    public static function from(array $definitions, array $readModels, array $checkpoints): array
    {
        $rows = 0;
        foreach ($readModels as $model) {
            $rows += count((array) ($model['rows'] ?? []));
        }

        $processed = 0;
        $lastSequence = 0;
        foreach ($checkpoints as $checkpoint) {
            $processed += (int) ($checkpoint['processed'] ?? 0);
            $lastSequence = max($lastSequence, (int) ($checkpoint['lastSequence'] ?? 0));
        }

        return [
            'projections' => count($definitions),
            'readModels' => count($readModels),
            'rows' => $rows,
            'processed' => $processed,
            'lastSequence' => $lastSequence,
        ];
    }
}
