<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Sourcing\Verification;

final class EventSourcingRuntimeLockManager
{
    private EventSourcingRuntimeStore $store;

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

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

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