<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\PageBuilder\Command;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Messenger\Messaging\CommandHandlerInterface;
use CpCreation\VitiCore\PageBuilder\Builder\BuilderTailwindCompiler;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final readonly class CompileBuilderTailwindClassesCommandHandler implements CommandHandlerInterface
{
    public function __construct(
        private BuilderTailwindCompiler $compiler,
    ) {
    }

    public function __invoke(CompileBuilderTailwindClassesCommand $command): void
    {
        $result = $this->compiler->compile();

        if (true !== $result['compiled'] && true !== ($result['skipped'] ?? false)) {
            throw new \RuntimeException($result['error'] ?? 'Compilation Tailwind builder impossible.');
        }
    }
}
