using System.Text.Json;

namespace Elfisa.Core;

public sealed class AppSettings
{
    public string? ApiBaseUrl { get; set; }
    public string Theme { get; set; } = "Light";        // Light | Dark
    public string? DefaultLocationId { get; set; }
    public string? Token { get; set; }                   // сохранённая сессия
    public string? Role { get; set; }
    public string? Username { get; set; }
}

/// <summary>Настройки приложения в %APPDATA%\ElfisaAvalonia\settings.json.</summary>
public static class SettingsStore
{
    private static readonly string Dir =
        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ElfisaAvalonia");
    private static readonly string File = Path.Combine(Dir, "settings.json");

    public static AppSettings Current { get; private set; } = Load();

    public static AppSettings Load()
    {
        try
        {
            if (System.IO.File.Exists(File))
                return JsonSerializer.Deserialize<AppSettings>(System.IO.File.ReadAllText(File)) ?? new AppSettings();
        }
        catch { /* повреждённый файл — начинаем с чистых настроек */ }
        return new AppSettings();
    }

    public static void Save()
    {
        try
        {
            Directory.CreateDirectory(Dir);
            System.IO.File.WriteAllText(File, JsonSerializer.Serialize(Current, new JsonSerializerOptions { WriteIndented = true }));
        }
        catch { /* нет прав/диска — молча пропускаем */ }
    }
}
