<?php

namespace App\ScheduledTasks;

use Mtc\MercuryDataModels\ScheduledTaskRun;

/**
 * Carries earlier action results along the chain so later actions (e.g. a summary
 * email) can reference what happened before them in the same run.
 */
class TaskRunContext
{
    /** @var array<int, array<string, mixed>> */
    private array $entries = [];

    public function __construct(public readonly ScheduledTaskRun $run)
    {
    }

    public function push(string $actionType, ActionResult $result): void
    {
        $this->entries[] = array_merge(['action' => $actionType], $result->toArray());
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function entries(): array
    {
        return $this->entries;
    }

    public function hasFailure(): bool
    {
        return collect($this->entries)
            ->contains(fn (array $entry) => $entry['status'] === ActionResult::STATUS_FAILED);
    }
}
