<?php

namespace App\ScheduledTasks;

use App\ScheduledTasks\Contracts\TaskAction;
use Illuminate\Support\Collection;

class ActionRegistry
{
    /**
     * All registered actions, keyed by registry key.
     *
     * @return Collection<string, TaskAction>
     */
    public function all(): Collection
    {
        return collect(config('scheduled_tasks.actions', []))
            ->map(fn (string $class) => app($class));
    }

    /**
     * Actions an admin may pick when composing a task: available for the
     * tenant and not system-only.
     *
     * @return Collection<string, TaskAction>
     */
    public function selectable(): Collection
    {
        return $this->all()
            ->filter(fn (TaskAction $action) => $action->userSelectable() && $action->available());
    }

    public function resolve(string $key): ?TaskAction
    {
        $class = config("scheduled_tasks.actions.{$key}");

        return $class ? app($class) : null;
    }
}
