<?php

declare(strict_types=1);

namespace App\State\Provider\Access;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResources\Access\AdminAccess;
use App\Entity\Access\Access;
use App\Service\Access\AdminAccessUtils;
use Exception;
use RuntimeException;
use Symfony\Bundle\SecurityBundle\Security;

/**
 * Class AdminAccessProvider : custom provider pour la resource AdminAccess
 * @package App\DataProvider\Access
 */
final class AdminAccessProvider implements ProviderInterface
{
    public function __construct(
        private Security $security,
        private AdminAccessUtils $adminAccessUtils
    )
    { }

    /**
     * @param Operation $operation
     * @param mixed[] $uriVariables
     * @param mixed[] $context
     * @return AdminAccess|null
     * @throws Exception
     */
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?AdminAccess
    {
        if($operation instanceof GetCollection) {
            throw new RuntimeException('not supported', 500);
        }

        /** @var Access $access */
        $access = $this->security->getUser();
        return $this->adminAccessUtils->getAdminAccess($access->getOrganization());
    }
}