<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\Verification;

final class WorkflowFullRuntimeLockManager
{
    private WorkflowFullRuntimeStore $store;

    public function __construct(private readonly string $basePath)
    {
        $this->store = new WorkflowFullRuntimeStore($basePath);
    }

    public function verify(): WorkflowFullRuntimeReport
    {
        $report = (new WorkflowFullRuntimeVerifier($this->basePath))->verify();
        return $this->store->saveReport($report);
    }

    public function lock(): WorkflowFullRuntimeLock
    {
        $report = $this->verify();
        $lock = WorkflowFullRuntimeLock::create($report);
        return $this->store->saveLock($lock);
    }

    public function status(): array
    {
        $report = $this->store->loadReport();
        $lock = $this->store->loadLock();

        return [
            'report' => $report?->toArray(),
            'lock' => $lock?->toArray(),
            'reportFile' => $this->store->reportFile(),
            'lockFile' => $this->store->lockFile(),
            'reportExists' => $this->store->reportExists(),
            'lockExists' => $this->store->lockExists(),
        ];
    }

    public function store(): WorkflowFullRuntimeStore
    {
        return $this->store;
    }
}
