<?php

namespace App\Support;

use Illuminate\Support\Facades\File;

class GcmsVersion
{
    public static function lockPath(): string
    {
        return storage_path('app/gcms-version.lock');
    }

    public static function current(): string
    {
        $path = self::lockPath();
        if (is_readable($path)) {
            $v = trim(File::get($path));
            if ($v !== '' && preg_match('/^[0-9]+\.[0-9]+\.[0-9]+/', $v)) {
                return $v;
            }
        }

        return (string) config('cms.version', '1.0.0');
    }

    public static function write(string $version): void
    {
        File::ensureDirectoryExists(dirname(self::lockPath()));
        File::put(self::lockPath(), $version.PHP_EOL);
    }
}
