using ClaudeDo.Localization;
using ClaudeDo.Ui;
using ClaudeDo.Ui.ViewModels.Modals.Settings;

namespace ClaudeDo.Ui.Tests;

public class LanguageSettingTests
{
    private static Localizer Make()
    {
        var dir = Path.Combine(Path.GetTempPath(), "loc_" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(dir);
        File.WriteAllText(Path.Combine(dir, "en.json"),
            """{ "metadata": { "code": "en", "name": "English" }, "settings": { "save": "Save" } }""");
        File.WriteAllText(Path.Combine(dir, "de.json"),
            """{ "metadata": { "code": "de", "name": "Deutsch" }, "settings": { "save": "Speichern" } }""");
        return new Localizer(LocaleStore.Load(dir), "en");
    }

    [Fact]
    public void Selecting_language_switches_localizer_and_persists()
    {
        var loc = Make();
        var settings = new AppSettings();
        var saved = false;
        var vm = new GeneralSettingsTabViewModel(loc, code => { settings.Language = code; saved = true; });

        var de = vm.Languages.First(l => l.Code == "de");
        vm.SelectedLanguage = de;

        Assert.Equal("de", loc.CurrentCode);
        Assert.True(saved);
        Assert.Equal("de", settings.Language);
    }
}
