<?php

namespace App\Support;

use Illuminate\Support\Facades\File;

class InstallationState
{
    public function lockPath(): string
    {
        return storage_path('app/'.config('installer.lock_file', 'installed.lock'));
    }

    public function isInstalled(): bool
    {
        return File::exists($this->lockPath());
    }

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

        if (! File::isDirectory($directory)) {
            File::makeDirectory($directory, 0755, true);
        }

        File::put($path, json_encode([
            'installed_at' => now()->toIso8601String(),
            'version' => config('installer.version'),
        ], JSON_PRETTY_PRINT));
    }

    public function clear(): void
    {
        if (File::exists($this->lockPath())) {
            File::delete($this->lockPath());
        }
    }
}
