<?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\Lifecycle\LifecycleEventVerifier;

final class LifecycleLockCommand extends AbstractCommand
{
    public function name(): string { return 'lifecycle:lock'; }
    public function description(): string { return 'Verify and lock lifecycle events.'; }

    public function handle(array $arguments = []): int
    {
        $base = ConsolePath::basePath();
        $result = (new LifecycleEventVerifier())->verify($base);
        $payload = [
            'version' => 'R0100.5C',
            'status' => $result['ok'] ? 'locked' : 'failed',
            'lockedAt' => date('c'),
            'verification' => $result,
            'nextMilestone' => 'R0100.5D Event Compiler',
        ];
        $file = $base . '/storage/reports/r0100/r0100-lifecycle-events-lock.json';
        if (!is_dir(dirname($file))) {
            mkdir(dirname($file), 0775, true);
        }
        file_put_contents($file, json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
        $this->title('PPphp Lifecycle Events Lock');
        $this->line('Status: ' . ($result['ok'] ? 'LOCKED' : 'FAILED'));
        $this->line('File: storage/reports/r0100/r0100-lifecycle-events-lock.json');
        return $result['ok'] ? 0 : 1;
    }
}
