<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\HumanTask;

final class HumanTaskStatistics
{
    public static function from(array $tasks, array $assignments): array
    {
        $open = 0; $assigned = 0; $completed = 0; $successfulAssignments = 0;

        foreach ($tasks as $task) {
            $status = (string) ($task['status'] ?? 'open');
            if ($status === 'completed') { $completed++; }
            elseif ($status === 'assigned') { $assigned++; }
            else { $open++; }
        }

        foreach ($assignments as $assignment) {
            if (($assignment['successful'] ?? false) === true) {
                $successfulAssignments++;
            }
        }

        return [
            'tasks' => count($tasks),
            'open' => $open,
            'assigned' => $assigned,
            'completed' => $completed,
            'assignments' => count($assignments),
            'successfulAssignments' => $successfulAssignments,
        ];
    }
}
