using ClaudeDo.Ui.Services;
using ClaudeDo.Ui.ViewModels.Modals.Settings;
using Xunit;

namespace ClaudeDo.Ui.Tests.ViewModels;

public class GeneralSettingsTabViewModelTests
{
    private sealed class FakeWorker : StubWorkerClient
    {
        public List<SessionSkillDto> Installed = new();
        public override Task<List<SessionSkillDto>> GetSessionSkillsAsync() => Task.FromResult(Installed);
    }

    [Fact]
    public async Task LoadSessionSkillsAsync_reflects_current_selection()
    {
        var w = new FakeWorker
        {
            Installed = new() { new SessionSkillDto("a", "", "url-a", "ref", DateTimeOffset.UtcNow),
                                 new SessionSkillDto("b", "", "url-b", "ref", DateTimeOffset.UtcNow) },
        };
        var vm = new GeneralSettingsTabViewModel();

        await vm.LoadSessionSkillsAsync(w, new List<string> { "b" });

        Assert.Equal(2, vm.SessionSkills.Count);
        Assert.False(vm.SessionSkills.Single(s => s.Name == "a").IsSelected);
        Assert.True(vm.SessionSkills.Single(s => s.Name == "b").IsSelected);
    }

    [Fact]
    public async Task SelectedSessionSkillNames_round_trips_through_toggle()
    {
        var w = new FakeWorker
        {
            Installed = new() { new SessionSkillDto("a", "", "url-a", "ref", DateTimeOffset.UtcNow) },
        };
        var vm = new GeneralSettingsTabViewModel();
        await vm.LoadSessionSkillsAsync(w, null);

        Assert.Null(vm.SelectedSessionSkillNames());

        vm.SessionSkills.Single().IsSelected = true;

        Assert.Equal(new List<string> { "a" }, vm.SelectedSessionSkillNames());
    }
}
