using SilverOS.Welcome.Core.Flavours;
using Xunit;

public class FlavourLoaderTests
{
    private static string WriteTemp(params (string name, string json)[] files)
    {
        var dir = Directory.CreateTempSubdirectory("flav").FullName;
        foreach (var (name, json) in files) File.WriteAllText(Path.Combine(dir, name), json);
        return dir;
    }

    [Fact]
    public void Loads_all_manifests_sorted_with_default_first()
    {
        var dir = WriteTemp(
            ("privacy-max.json", """{ "id":"privacy-max","label":"Privacy-Max","hardening":{"modules":["00"]} }"""),
            ("daily-driver.json", """{ "id":"daily-driver","label":"Daily-Driver","isDefault":true,"hardening":{"modules":["00"]} }"""));
        var loaded = new FlavourLoader().Load(dir);
        Assert.Equal(2, loaded.Count);
        Assert.Equal("daily-driver", loaded[0].Id); // default first
    }

    [Fact]
    public void Throws_when_a_manifest_has_no_id()
    {
        var dir = WriteTemp(("bad.json", """{ "label":"No Id","hardening":{"modules":["00"]} }"""));
        var ex = Assert.Throws<FlavourValidationException>(() => new FlavourLoader().Load(dir));
        Assert.Contains("bad.json", ex.Message);
    }

    [Fact]
    public void Throws_when_no_default_flavour_present()
    {
        var dir = WriteTemp(("a.json", """{ "id":"a","label":"A","hardening":{"modules":["00"]} }"""));
        Assert.Throws<FlavourValidationException>(() => new FlavourLoader().Load(dir));
    }
}
