<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\Verification;

final class WorkflowRuntimeLockManager
{
    private WorkflowRuntimeStore $store;

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

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

    public function lock(): WorkflowRuntimeLock
    {
        $report = $this->verify();
        $lock = WorkflowRuntimeLock::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(): WorkflowRuntimeStore
    {
        return $this->store;
    }
}
