<?php

namespace MintyPHP\Service\Access;

trait AuthorizationPolicyContextTrait
{
    protected function actorUserId(array $context): int
    {
        return (int) ($context['actor_user_id'] ?? 0);
    }

    protected function hasPermission(int $userId, string $permissionKey): bool
    {
        return $userId > 0 && $this->permissionService->userHas($userId, $permissionKey);
    }

    protected function capabilitiesFromDecision(AuthorizationDecision $decision): array
    {
        $capabilities = $decision->attribute('capabilities', []);
        return is_array($capabilities) ? $capabilities : [];
    }
}
