<?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\DI\Verification\DiSubsystemLock;
use PpPhp\Core\Platform\DI\Verification\DiSubsystemVerifier;

final class ContainerLockCommand extends AbstractCommand
{
    public function name(): string { return 'container:lock'; }
    public function description(): string { return 'Verify and lock the R0100.4 DI subsystem.'; }

    public function handle(array $arguments = []): int
    {
        $base = ConsolePath::basePath();
        $status = (new DiSubsystemVerifier())->verify($base);
        $payload = DiSubsystemLock::payload($status);

        $file = $base . '/' . DiSubsystemLock::LOCK_FILE;
        $dir = dirname($file);

        if (!is_dir($dir)) {
            mkdir($dir, 0775, true);
        }

        file_put_contents($file, json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

        $this->title('PPphp DI Subsystem Lock');
        $this->line('Status: ' . ($status->ok() ? 'LOCKED' : 'FAILED'));
        $this->line('File: ' . DiSubsystemLock::LOCK_FILE);

        return $status->ok() ? 0 : 1;
    }
}
