<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow;

final class WorkflowStatistics
{
    public static function from(array $definitions, array $instances, array $tasks, array $executions): array
    {
        $running = 0;
        $completed = 0;
        $failed = 0;
        $pendingTasks = 0;
        $completedTasks = 0;
        $successfulExecutions = 0;

        foreach ($instances as $instance) {
            $status = (string) ($instance['status'] ?? 'running');
            if ($status === 'completed') { $completed++; }
            elseif ($status === 'failed') { $failed++; }
            else { $running++; }
        }

        foreach ($tasks as $task) {
            if (($task['status'] ?? 'pending') === 'completed') { $completedTasks++; }
            else { $pendingTasks++; }
        }

        foreach ($executions as $execution) {
            if (($execution['successful'] ?? false) === true) { $successfulExecutions++; }
        }

        return [
            'workflows' => count($definitions),
            'instances' => count($instances),
            'running' => $running,
            'completed' => $completed,
            'failed' => $failed,
            'tasks' => count($tasks),
            'pendingTasks' => $pendingTasks,
            'completedTasks' => $completedTasks,
            'executions' => count($executions),
            'successfulExecutions' => $successfulExecutions,
        ];
    }
}
