using ClaudeDo.Data;
using ClaudeDo.Ui.Services;
using ClaudeDo.Ui.ViewModels.Islands;
using Microsoft.EntityFrameworkCore;

namespace ClaudeDo.Ui.Tests.ViewModels;

public class DetailsIslandEmptyStateTests : IDisposable
{
    private readonly string _dbPath;

    public DetailsIslandEmptyStateTests()
    {
        _dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_emptystate_test_{Guid.NewGuid():N}.db");
        using var ctx = NewContext();
        ctx.Database.EnsureCreated();
    }

    public void Dispose()
    {
        try { File.Delete(_dbPath); } catch { }
        try { File.Delete(_dbPath + "-wal"); } catch { }
        try { File.Delete(_dbPath + "-shm"); } catch { }
    }

    private ClaudeDoDbContext NewContext()
    {
        var opts = new DbContextOptionsBuilder<ClaudeDoDbContext>()
            .UseSqlite($"Data Source={_dbPath}")
            .Options;
        return new ClaudeDoDbContext(opts);
    }





    private DetailsIslandViewModel NewDetailsVm(StubWorkerClient stub)
    {
        var factory = new TestDbFactory(NewContext);
        return new DetailsIslandViewModel(factory, stub, new NullServiceProvider(), new ClaudeDo.Ui.Services.MergeCoordinator());
    }

    [Fact]
    public void IsEmptyStateVisible_true_when_no_task_selected()
    {
        var vm = NewDetailsVm(new DefaultStub());

        Assert.True(vm.IsEmptyStateVisible);
    }

    [Fact]
    public void IsEmptyStateVisible_false_once_a_task_is_bound()
    {
        var vm = NewDetailsVm(new DefaultStub());
        var row = new TaskRowViewModel { Id = "t1", Title = "Task" };

        vm.Bind(row, "test");

        Assert.False(vm.IsEmptyStateVisible);
    }


    [Fact]
    public void IsEmptyStateVisible_false_in_prep_mode_even_without_a_task()
    {
        var vm = NewDetailsVm(new DefaultStub());

        vm.ShowPrep();

        Assert.True(vm.IsPrepMode);
        Assert.Null(vm.Task);
        Assert.False(vm.IsEmptyStateVisible);
    }

    [Fact]
    public void IsEmptyStateVisible_true_again_after_unbinding_a_task()
    {
        var vm = NewDetailsVm(new DefaultStub());
        var row = new TaskRowViewModel { Id = "t1", Title = "Task" };
        vm.Bind(row, "test");

        vm.Bind(null, "test");

        Assert.True(vm.IsEmptyStateVisible);
    }
}
