#!/usr/bin/env dotnet-script

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

// 读取 appsettings.json
var appSettingsPath = "./appsettings.json";

var content = File.ReadAllText(appSettingsPath);
Console.WriteLine("现在配置");
Console.WriteLine(content);

var json = JsonNode.Parse(content);

// 从环境变量获取 secrets
var connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION");
var webapi = Environment.GetEnvironmentVariable("WEBAPI_URL");

// 更新配置值
json["ConnectionStrings"]["DefaultConnection"] = connectionString;
json["WebApi"]["ServerUrl"] = webapi;

content = json.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("更新后的配置");
Console.WriteLine(content);
// 写回文件
File.WriteAllText(appSettingsPath, content);

Console.WriteLine("AppSettings updated successfully");


var appSettingsPath2 = "../Atomx.Admin.Client/wwwroot/appsettings.json";
var content2 = File.ReadAllText(appSettingsPath2);
Console.WriteLine("现在配置");
Console.WriteLine(content2);

var json2 = JsonNode.Parse(content2);

// 从环境变量获取 secrets
var webapi2 = Environment.GetEnvironmentVariable("WEBAPI_URL");

// 更新配置值
json2["WebApi"]["ServerUrl"] = webapi2;

content2 = json2.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("更新后的配置");
Console.WriteLine(content2);
// 写回文件
File.WriteAllText(appSettingsPath2, content2);

Console.WriteLine("AppSettings updated successfully");