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

namespace AutoIngest.Core
{
    public class AppConfig
    {
        public int JpgQuality { get; set; } = 92;
        public bool AutoImport { get; set; } = true;
    }

    public static class ConfigManager
    {
        static readonly string CONFIG_PATH = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
            ".autoingest_config.json");

        static readonly JsonSerializerOptions JSON_OPTS = new() { WriteIndented = true };

        public static AppConfig LoadConfig()
        {
            try
            {
                if (File.Exists(CONFIG_PATH))
                {
                    string json = File.ReadAllText(CONFIG_PATH);
                    return JsonSerializer.Deserialize<AppConfig>(json) ?? new AppConfig();
                }
            }
            catch (UnauthorizedAccessException) { }
            catch (IOException) { }
            catch (JsonException) { }
            return new AppConfig();
        }

        public static void SaveConfig(AppConfig config)
        {
            try
            {
                File.WriteAllText(CONFIG_PATH, JsonSerializer.Serialize(config, JSON_OPTS));
            }
            catch (UnauthorizedAccessException) { }
            catch (IOException) { }
        }
    }
}
