<?php

declare(strict_types=1);

namespace App\DataProvider\Access;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\ApiResources\Access\AdminAccess;
use App\Entity\Access\Access;
use App\Service\Access\AdminAccessUtils;
use Symfony\Component\Security\Core\Security;

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

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return AdminAccess::class === $resourceClass;
    }

    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?AdminAccess
    {
        /** @var Access $access */
        $access = $this->security->getUser();
        return $this->adminAccessUtils->getAdminAccess($access->getOrganization());
    }
}