<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Console\Commands;

use PpPhp\Core\Platform\Console\AbstractCommand;
use PpPhp\Core\Platform\Console\ConsolePath;
use PpPhp\Core\Platform\Event\Compiler\EventArtifactWriter;
use PpPhp\Core\Platform\Event\Compiler\EventCompilerPaths;
use PpPhp\Core\Platform\Event\Compiler\EventCompilerVerifier;

final class EventsCompilerLockCommand extends AbstractCommand
{
    public function name(): string { return 'events:compiler:lock'; }
    public function description(): string { return 'Verify and lock Event compiler subsystem.'; }

    public function handle(array $arguments = []): int
    {
        $base = ConsolePath::basePath();
        $result = (new EventCompilerVerifier())->verify($base);
        $payload = [
            'version' => 'R0100.5D',
            'status' => $result['ok'] ? 'locked' : 'failed',
            'lockedAt' => date('c'),
            'verification' => $result,
            'nextMilestone' => 'R0100.5E Async Queue & Deferred Events',
        ];
        (new EventArtifactWriter())->writeJson(EventCompilerPaths::lockFile($base), $payload);
        $this->title('PPphp Event Compiler Lock');
        $this->line('Status: ' . ($result['ok'] ? 'LOCKED' : 'FAILED'));
        $this->line('File: ' . EventCompilerPaths::LOCK_FILE);
        return $result['ok'] ? 0 : 1;
    }
}
