<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Worker\Monitoring\Lock;

final class WorkerMonitoringLockGenerator
{
    public function generate(string $basePath, array $verification): string
    {
        $reportDir = rtrim($basePath, DIRECTORY_SEPARATOR) . '/storage/reports/r0100';

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

        $lockFile = $reportDir . '/r0100-worker-monitoring-lock.json';

        file_put_contents($lockFile, json_encode([
            'milestone' => 'R0100.5E.5.5.6',
            'name' => 'Worker Monitoring & Recovery Verification Lock',
            'status' => ($verification['ok'] ?? false) ? 'LOCKED' : 'FAILED',
            'score' => $verification['summary']['score'] ?? 0,
            'verification' => $verification,
            'lockedAt' => date('c'),
        ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

        return $lockFile;
    }
}
