<?php

declare(strict_types=1);

namespace App\Module\Core\Infrastructure\ApiPlatform\State\Provider;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Module\Core\Infrastructure\ApiPlatform\Resource\AuditLogEntityTypesResource;
use Doctrine\DBAL\Connection;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
 * Provider DBAL : SELECT DISTINCT entity_type FROM audit_log.
 *
 * @implements ProviderInterface<AuditLogEntityTypesResource>
 */
final readonly class AuditLogEntityTypesProvider implements ProviderInterface
{
    public function __construct(
        #[Autowire(service: 'doctrine.dbal.default_connection')]
        private Connection $connection,
    ) {}

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): AuditLogEntityTypesResource
    {
        /** @var list<string> $types */
        $types = $this->connection
            ->executeQuery('SELECT DISTINCT entity_type FROM audit_log ORDER BY entity_type ASC')
            ->fetchFirstColumn()
        ;

        return new AuditLogEntityTypesResource(entityTypes: $types);
    }
}
