<?php

declare(strict_types=1);

namespace Apexl\Io\User\Hooks;

use Apexl\Io\Entity;
use Apexl\Io\Interfaces\AbstractHook;
use Apexl\Io\Services\EntityFinderService;
use Apexl\Io\User\Enums\PermissionAction;
use Apexl\Io\User\Services\EntityPermissionChecker;
use Apexl\Vault\Interfaces\QueryBuilderInterface;

final readonly class ApplyPermissionChecksToEntityFindQuery implements AbstractHook
{
    public function __construct(private EntityPermissionChecker $entityPermissionChecker) {}

    public function hooks(): array
    {
        return [EntityFinderService::HOOK__QUERY];
    }

    public function __invoke(QueryBuilderInterface $query, Entity $entity, ?string $tableAlias = null): void
    {
        $this->entityPermissionChecker->check($query, $entity, PermissionAction::READ, $tableAlias);
    }
}