namespace Flo.Installer.Provisioning;

internal sealed class FerretDbProvisioner(Paths paths)
{
    public ProcessRunner Start(int listenPort, int postgresPort, string pgUser, string pgPassword, string pgDatabase)
    {
        Directory.CreateDirectory(paths.FerretDbData);

        string pgUrl = $"postgres://{Uri.EscapeDataString(pgUser)}:{Uri.EscapeDataString(pgPassword)}@127.0.0.1:{postgresPort}/{pgDatabase}";

        var env = new Dictionary<string, string>
        {
            ["FERRETDB_POSTGRESQL_URL"] = pgUrl,
            ["FERRETDB_LISTEN_ADDR"]    = $"127.0.0.1:{listenPort}",
            ["FERRETDB_STATE_DIR"]      = paths.FerretDbData,
            ["FERRETDB_TELEMETRY"]      = "disable",
            ["FERRETDB_LOG_LEVEL"]      = "info",
        };

        return ProcessRunner.Start(paths.FerretDbExe(), [], env, name: "ferretdb");
    }
}
