<?php

declare(strict_types=1);

namespace App\Services\Install;

use Illuminate\Support\Facades\File;

final class DBErpInstallStatus
{
    public function isInstalled(): bool
    {
        if (filter_var(config('dberp.installed'), FILTER_VALIDATE_BOOLEAN)) {
            return true;
        }

        return is_file($this->lockFilePath());
    }

    public function lockFilePath(): string
    {
        return storage_path('app/dberp/installed.lock');
    }

    public function createLock(): void
    {
        $path = $this->lockFilePath();
        $directory = dirname($path);

        if (! is_dir($directory)) {
            File::ensureDirectoryExists($directory);
        }

        File::put($path, '');
    }

    public function removeLock(): void
    {
        $path = $this->lockFilePath();

        if (is_file($path)) {
            File::delete($path);
        }
    }
}
