using ClaudeDo.Localization;

namespace ClaudeDo.Localization.Tests;

public class LocaleStoreTests
{
    private static string WriteTempLocales(params (string file, string json)[] files)
    {
        var dir = Path.Combine(Path.GetTempPath(), "loc_" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(dir);
        foreach (var (file, json) in files)
            File.WriteAllText(Path.Combine(dir, file), json);
        return dir;
    }

    [Fact]
    public void Load_discovers_all_json_files()
    {
        var dir = WriteTempLocales(
            ("en.json", """{ "metadata": { "code": "en", "name": "English" }, "a": { "b": "x" } }"""),
            ("de.json", """{ "metadata": { "code": "de", "name": "Deutsch" }, "a": { "b": "y" } }"""));

        var store = LocaleStore.Load(dir);

        Assert.Equal(2, store.Available.Count);
        Assert.True(store.TryGet("en", out var en));
        Assert.Equal("x", en!.Strings["a.b"]);
        Assert.True(store.TryGet("de", out var de));
        Assert.Equal("Deutsch", de!.Name);
    }

    [Fact]
    public void Load_returns_empty_when_folder_missing()
    {
        var store = LocaleStore.Load(Path.Combine(Path.GetTempPath(), "does_not_exist_" + Guid.NewGuid()));
        Assert.Empty(store.Available);
    }
}
