using System.IO;
using System.Text.Json;

namespace NexusShell.Services;

public sealed record ClientVersionInfo(string Version, int Build)
{
    public static ClientVersionInfo LoadDefault()
    {
        var path = Path.Combine(AppContext.BaseDirectory, "config", "version.poc.json");
        if (!File.Exists(path))
        {
            return new ClientVersionInfo("0.1.0-poc1", 1);
        }

        var json = File.ReadAllText(path);
        var doc = JsonSerializer.Deserialize<VersionDoc>(json, JsonOptions);
        return new ClientVersionInfo(doc?.Version ?? "0.1.0-poc1", doc?.Build ?? 1);
    }

    private sealed class VersionDoc
    {
        public string Version { get; set; } = "0.1.0-poc1";
        public int Build { get; set; } = 1;
    }

    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        PropertyNameCaseInsensitive = true,
    };
}
