using System.Text.Json;

namespace VPNAuth.Server;

public class ConfigUser
{
    public string? Username { get; set; }
    public List<string>? Ips { get; set; }
}

public class ConfigApp
{
    public string? ClientId { get; set; }
    public string? RedirectUri { get; set; }
    public string? Secret { get; set; }
}

public class Config
{
    public List<ConfigUser>? Users { get; set; }
    public List<ConfigApp>? Apps { get; set; }

    public ConfigApp? FindApp(string clientId)
        => Apps?.Find(app => app.ClientId == clientId);

    private static string _filePath = "./config.json";

    public static void CreateIfNotExists()
    {
        if (File.Exists(_filePath)) return;

        File.Create(_filePath);
        File.WriteAllText(_filePath, JsonSerializer.Serialize(new Config
        {
            Users = []
        }));
    }

    public static Config Read()
        => JsonSerializer.Deserialize<Config>(File.ReadAllText(_filePath))!;
}