using System.Text.Json;
using System.Text.Json.Serialization;

namespace Flo.Installer.Provisioning;

internal sealed record FloConfig(
    [property: JsonPropertyName("postgresPort")] int PostgresPort,
    [property: JsonPropertyName("ferretDbPort")] int FerretDbPort,
    [property: JsonPropertyName("postgresSuperuser")] string PostgresSuperuser,
    [property: JsonPropertyName("applicationDbRole")] string ApplicationDbRole,
    [property: JsonPropertyName("applicationDbName")] string ApplicationDbName)
{
    public static FloConfig Load(string path)
        => JsonSerializer.Deserialize<FloConfig>(File.ReadAllBytes(path))
           ?? throw new InvalidOperationException($"Could not parse {path}");

    public void Save(string path)
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path)!);
        File.WriteAllText(path, JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true }));
    }
}
