using ClaudeDo.Data.Models;
using ClaudeDo.Worker.Tests.Infrastructure;
using ClaudeDo.Worker.Usage;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Worker.Tests.Usage;

public sealed class RunningTaskProbeTests : IDisposable
{
    private readonly DbFixture _db = new();
    private int _numberSeed;

    public void Dispose() => _db.Dispose();

    private async Task SeedTaskAsync(TaskStatus status)
    {
        await using var context = _db.CreateFactory().CreateDbContext();
        var list = new ListEntity { Id = Guid.NewGuid().ToString(), Name = "L", CreatedAt = DateTime.UtcNow };
        context.Lists.Add(list);
        context.Tasks.Add(new TaskEntity
        {
            Id = Guid.NewGuid().ToString(),
            ListId = list.Id,
            Title = $"task-{status}",
            Number = ++_numberSeed,
            Status = status,
            CreatedAt = DateTime.UtcNow,
        });
        await context.SaveChangesAsync();
    }

    [Fact]
    public async Task AnyRunningAsync_NoTasks_ReturnsFalse()
    {
        var probe = new RunningTaskProbe(_db.CreateFactory());

        Assert.False(await probe.AnyRunningAsync());
    }

    [Fact]
    public async Task AnyRunningAsync_QueuedTaskOnly_ReturnsFalse()
    {
        await SeedTaskAsync(TaskStatus.Queued);
        var probe = new RunningTaskProbe(_db.CreateFactory());

        Assert.False(await probe.AnyRunningAsync());
    }

    [Fact]
    public async Task AnyRunningAsync_RunningTask_ReturnsTrue()
    {
        await SeedTaskAsync(TaskStatus.Idle);
        await SeedTaskAsync(TaskStatus.Running);
        var probe = new RunningTaskProbe(_db.CreateFactory());

        Assert.True(await probe.AnyRunningAsync());
    }
}
