using SharepointToolbox.Core.Models;
using SharepointToolbox.Infrastructure.Persistence;

namespace SharepointToolbox.Services;

public class SettingsService
{
    private readonly SettingsRepository _repository;

    private static readonly HashSet<string> SupportedLanguages = new(StringComparer.OrdinalIgnoreCase)
    {
        "en", "fr"
    };

    public SettingsService(SettingsRepository repository)
    {
        _repository = repository;
    }

    public Task<AppSettings> GetSettingsAsync()
        => _repository.LoadAsync();

    public async Task SetLanguageAsync(string cultureCode)
    {
        if (!SupportedLanguages.Contains(cultureCode))
            throw new ArgumentException($"Unsupported language code '{cultureCode}'. Supported: en, fr.", nameof(cultureCode));

        var settings = await _repository.LoadAsync();
        settings.Lang = cultureCode;
        await _repository.SaveAsync(settings);
    }

    public async Task SetDataFolderAsync(string path)
    {
        var settings = await _repository.LoadAsync();
        settings.DataFolder = path;
        await _repository.SaveAsync(settings);
    }

    public async Task SetAutoTakeOwnershipAsync(bool enabled)
    {
        var settings = await _repository.LoadAsync();
        settings.AutoTakeOwnership = enabled;
        await _repository.SaveAsync(settings);
    }

    public async Task SetThemeAsync(string mode)
    {
        if (mode is not ("System" or "Light" or "Dark"))
            throw new ArgumentException($"Unsupported theme '{mode}'. Supported: System, Light, Dark.", nameof(mode));

        var settings = await _repository.LoadAsync();
        settings.Theme = mode;
        await _repository.SaveAsync(settings);
    }
}
