<?php
declare(strict_types=1);

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

final class QueryStatistics
{
    public static function from(array $definitions, array $results): array
    {
        $totalReturned = 0;
        $totalRows = 0;
        $totalTime = 0.0;

        foreach ($results as $result) {
            $totalReturned += (int) ($result['returned'] ?? 0);
            $totalRows += (int) ($result['total'] ?? 0);
            $totalTime += (float) ($result['queryTimeMs'] ?? 0);
        }

        return [
            'readModels' => count($definitions),
            'queries' => count($results),
            'totalRows' => $totalRows,
            'returnedRows' => $totalReturned,
            'averageQueryTimeMs' => count($results) > 0 ? round($totalTime / count($results), 3) : 0,
            'throughput' => $totalTime > 0 ? round(($totalReturned / max(0.001, $totalTime)) * 1000, 2) : 0,
        ];
    }
}
