<?php

namespace NoahVet\Reef\Application\Security\UseCase;

use NoahVet\Reef\Application\Security\Service\PolicyOrchestratorInterface;
use NoahVet\Reef\Domain\Security\Policy\PolicyResult;

/**
 * Computes all applicable policy results for a subject without syncing to the IAM.
 * Use this to inspect what grants would be applied.
 */
class ExecutePoliciesUseCase implements ExecutePoliciesUseCaseInterface
{
    public function __construct(
        protected readonly PolicyOrchestratorInterface $orchestrator,
    ) {
    }

    /**
     * @return list<PolicyResult>
     */
    #[\Override]
    public function execute(object $subject): array
    {
        $results = [];

        foreach ($this->orchestrator->getPoliciesFor($subject) as $policy) {
            $results[] = $policy->apply($subject);
        }

        return $results;
    }
}
