<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\HumanTask;

final class HumanTaskInbox
{
    /** @param HumanTaskRecord[] $tasks */
    public function __construct(private readonly array $tasks = []) {}

    public function forUser(string $userId, array $groups = []): array
    {
        return array_values(array_filter($this->tasks, static fn (HumanTaskRecord $task): bool => !$task->isCompleted() && $task->visibleTo($userId, $groups)));
    }

    public function assignedTo(string $userId): array
    {
        return array_values(array_filter($this->tasks, static fn (HumanTaskRecord $task): bool => !$task->isCompleted() && $task->assignee() === $userId));
    }

    public function open(): array
    {
        return array_values(array_filter($this->tasks, static fn (HumanTaskRecord $task): bool => !$task->isCompleted() && $task->assignee() === ''));
    }
}
