using ClaudeDo.Data;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Config;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Runner;
using ClaudeDo.Worker.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Worker.Tests.Runner;

/// Verifies that RunAsync and ContinueAsync abort cleanly when StartRunningAsync fails
/// (e.g. the task is already Running due to a concurrent RunNow / QueuePicker race).
public sealed class StartRunningGuardTests : IDisposable
{
    private readonly DbFixture _db = new();
    private readonly string _tempDir;
    private readonly WorkerConfig _cfg;

    public StartRunningGuardTests()
    {
        _tempDir = Path.Combine(Path.GetTempPath(), $"cd_guard_{Guid.NewGuid():N}");
        Directory.CreateDirectory(_tempDir);
        _cfg = new WorkerConfig { SandboxRoot = _tempDir, LogRoot = _tempDir };
    }

    public void Dispose() { _db.Dispose(); try { Directory.Delete(_tempDir, true); } catch { } }

    private TaskRunner BuildRunner(IClaudeProcess claude)
    {
        var dbFactory = _db.CreateFactory();
        var state = TaskStateServiceBuilder.Build(dbFactory).State;
        var wt = new WorktreeManager(new ClaudeDo.Data.Git.GitService(), dbFactory, _cfg, NullLogger<WorktreeManager>.Instance);
        return new TaskRunner(claude, dbFactory, new HubBroadcaster(new CapturingHubContext()), wt,
            new ClaudeArgsBuilder(), _cfg, NullLogger<TaskRunner>.Instance, state, new TaskRunTokenRegistry(), new AttachmentStore(), new FakeSessionSkillSeeder(), new FakeTranscriptUsageReader());
    }

    [Fact]
    public async Task RunAsync_TaskAlreadyRunning_NoProcessStarted_NoRunRecord()
    {
        string listId = Guid.NewGuid().ToString(), taskId = Guid.NewGuid().ToString();
        using (var ctx = _db.CreateContext())
        {
            ctx.Lists.Add(new ListEntity { Id = listId, Name = "L", WorkingDir = null, CreatedAt = DateTime.UtcNow });
            ctx.Tasks.Add(new TaskEntity
            {
                Id = taskId, ListId = listId, Title = "Already running",
                Status = TaskStatus.Running, CreatedAt = DateTime.UtcNow,
            });
            await ctx.SaveChangesAsync();
        }

        var fake = new FakeClaudeProcess();
        var runner = BuildRunner(fake);

        using (var ctx = _db.CreateContext())
            await runner.RunAsync((await new TaskRepository(ctx).GetByIdAsync(taskId))!, "slot-1", CancellationToken.None);

        Assert.Equal(0, fake.CallCount);

        using var verify = _db.CreateContext();
        Assert.Empty(await new TaskRunRepository(verify).GetByTaskIdAsync(taskId));
    }

    [Fact]
    public async Task ContinueAsync_TaskAlreadyRunning_NoProcessStarted_NoNewRunRecord()
    {
        string listId = Guid.NewGuid().ToString(), taskId = Guid.NewGuid().ToString();
        using (var ctx = _db.CreateContext())
        {
            ctx.Lists.Add(new ListEntity { Id = listId, Name = "L", WorkingDir = null, CreatedAt = DateTime.UtcNow });
            ctx.Tasks.Add(new TaskEntity
            {
                Id = taskId, ListId = listId, Title = "Already running",
                Status = TaskStatus.Running, CreatedAt = DateTime.UtcNow,
            });
            await ctx.SaveChangesAsync();

            await new TaskRunRepository(ctx).AddAsync(new TaskRunEntity
            {
                Id = Guid.NewGuid().ToString(), TaskId = taskId, RunNumber = 1,
                IsRetry = false, Prompt = "p", SessionId = "sess-guard-test",
                StartedAt = DateTime.UtcNow.AddMinutes(-5), FinishedAt = DateTime.UtcNow.AddMinutes(-1),
                ExitCode = 0, ResultMarkdown = "ok",
            });
        }

        var fake = new FakeClaudeProcess();
        var runner = BuildRunner(fake);

        await runner.ContinueAsync(taskId, "follow up", "slot-1", CancellationToken.None);

        Assert.Equal(0, fake.CallCount);

        using var verify = _db.CreateContext();
        Assert.Single(await new TaskRunRepository(verify).GetByTaskIdAsync(taskId));
    }
}
