using ClaudeDo.Data;
using ClaudeDo.Data.Git;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Config;
using ClaudeDo.Worker.External;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Lifecycle;
using ClaudeDo.Worker.Queue;
using ClaudeDo.Worker.Runner;
using ClaudeDo.Worker.Tests.Infrastructure;
using ClaudeDo.Worker.Worktrees;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Worker.Tests.External;

public sealed class BatchMcpToolsTests : IDisposable
{
    private readonly DbFixture _db = new();
    private readonly ClaudeDoDbContext _ctx;
    private readonly TaskRepository _tasks;
    private readonly ListRepository _lists;
    private readonly HubBroadcaster _broadcaster;

    public BatchMcpToolsTests()
    {
        _ctx = _db.CreateContext();
        _tasks = new TaskRepository(_ctx);
        _lists = new ListRepository(_ctx);
        _broadcaster = new HubBroadcaster(new CapturingHubContext());
    }

    public void Dispose()
    {
        _ctx.Dispose();
        _db.Dispose();
    }

    private async Task<string> SeedListAsync()
    {
        var id = Guid.NewGuid().ToString();
        await _lists.AddAsync(new ListEntity { Id = id, Name = "L", CreatedAt = DateTime.UtcNow });
        return id;
    }

    private async Task<TaskEntity> SeedTaskAsync(string listId, string title = "t", TaskStatus status = TaskStatus.Idle)
    {
        var task = new TaskEntity
        {
            Id = Guid.NewGuid().ToString(),
            ListId = listId,
            Title = title,
            Status = status,
            CreatedAt = DateTime.UtcNow,
            CommitType = "chore",
        };
        await _tasks.AddAsync(task);
        return task;
    }

    private BatchMcpTools BuildSut()
    {
        var git = new GitService();
        var factory = _db.CreateFactory();
        var maintenance = new WorktreeMaintenanceService(factory, git, NullLogger<WorktreeMaintenanceService>.Instance);
        var merge = new TaskMergeService(factory, git, _broadcaster, TaskStateServiceBuilder.Build(factory).State, NullLogger<TaskMergeService>.Instance);
        var svc = new ExternalMcpService(
            _tasks, _lists, CreateQueue(), _broadcaster,
            TaskStateServiceBuilder.Build(factory).State,
            git, factory, maintenance, merge);
        return new BatchMcpTools(svc);
    }

    private QueueService CreateQueue()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), $"claudedo_batch_{Guid.NewGuid():N}");
        Directory.CreateDirectory(tempDir);
        var cfg = new WorkerConfig
        {
            SandboxRoot = Path.Combine(tempDir, "sandbox"),
            LogRoot = Path.Combine(tempDir, "logs"),
            QueueBackstopIntervalMs = 50,
        };
        var dbFactory = _db.CreateFactory();
        var broadcaster = new HubBroadcaster(new CapturingHubContext());
        var wtManager = new WorktreeManager(new GitService(), dbFactory, cfg, NullLogger<WorktreeManager>.Instance);
        var state = TaskStateServiceBuilder.Build(dbFactory).State;
        var runner = new TaskRunner(new FakeClaudeProcess(), dbFactory, broadcaster, wtManager, new ClaudeArgsBuilder(), cfg,
            NullLogger<TaskRunner>.Instance, state, new TaskRunTokenRegistry(), new AttachmentStore(), new FakeSessionSkillSeeder());
        var overrideSlot = new OverrideSlotService(dbFactory, runner, NullLogger<OverrideSlotService>.Instance);
        return new QueueService(dbFactory, runner, cfg, NullLogger<QueueService>.Instance,
            new QueueWaker(), new QueuePicker(dbFactory), overrideSlot, state);
    }

    [Fact]
    public async Task BatchAddTasks_CreatesAll_AndReportsOkPerItem()
    {
        var listId = await SeedListAsync();
        var sut = BuildSut();

        var results = await sut.BatchAddTasks(listId, new[]
        {
            new BatchAddTaskInput("a"),
            new BatchAddTaskInput("b"),
            new BatchAddTaskInput("c"),
        }, cancellationToken: CancellationToken.None);

        Assert.Equal(3, results.Count);
        Assert.All(results, r => Assert.True(r.Ok));
        Assert.Equal(new[] { 0, 1, 2 }, results.Select(r => r.Index).ToArray());
        var inList = await _tasks.GetByListIdAsync(listId);
        Assert.Equal(3, inList.Count);
    }

    [Fact]
    public async Task BatchAddTasks_FailingItem_DoesNotAbortTheRest()
    {
        var listId = await SeedListAsync();
        var sut = BuildSut();

        var results = await sut.BatchAddTasks(listId, new[]
        {
            new BatchAddTaskInput("ok-1"),
            new BatchAddTaskInput("   "),  // blank title → AddTask throws
            new BatchAddTaskInput("ok-2"),
        }, cancellationToken: CancellationToken.None);

        Assert.True(results[0].Ok);
        Assert.False(results[1].Ok);
        Assert.NotNull(results[1].Error);
        Assert.True(results[2].Ok);
        var inList = await _tasks.GetByListIdAsync(listId);
        Assert.Equal(2, inList.Count);
    }

    [Fact]
    public async Task BatchGetTasks_MissingId_IsFoundFalseNotError()
    {
        var listId = await SeedListAsync();
        var task = await SeedTaskAsync(listId);
        var sut = BuildSut();

        var results = await sut.BatchGetTasks(new[] { task.Id, "nope" }, CancellationToken.None);

        var found = results.Single(r => r.Id == task.Id);
        var missing = results.Single(r => r.Id == "nope");
        Assert.True(found.Found);
        Assert.NotNull(found.Task);
        Assert.False(missing.Found);
        Assert.Null(missing.Task);
        Assert.Null(missing.Error);
    }

    [Fact]
    public async Task BatchDeleteTasks_RunningTask_ReportedNotOk_OthersDeleted()
    {
        var listId = await SeedListAsync();
        var deletable = await SeedTaskAsync(listId, "del", TaskStatus.Idle);
        var running = await SeedTaskAsync(listId, "run", TaskStatus.Running);
        var sut = BuildSut();

        var results = await sut.BatchDeleteTasks(new[] { deletable.Id, running.Id }, CancellationToken.None);

        Assert.True(results.Single(r => r.TaskId == deletable.Id).Ok);
        Assert.False(results.Single(r => r.TaskId == running.Id).Ok);
        Assert.Null(await _tasks.GetByIdAsync(deletable.Id));
        Assert.NotNull(await _tasks.GetByIdAsync(running.Id));
    }

    [Fact]
    public async Task BatchUpdateTaskStatus_QueuesIdleTasks()
    {
        var listId = await SeedListAsync();
        var t1 = await SeedTaskAsync(listId, "a", TaskStatus.Idle);
        var t2 = await SeedTaskAsync(listId, "b", TaskStatus.Idle);
        var sut = BuildSut();

        var results = await sut.BatchUpdateTaskStatus(new[] { t1.Id, t2.Id }, "Queued", CancellationToken.None);

        Assert.All(results, r => Assert.True(r.Ok));
        Assert.Equal(TaskStatus.Queued, (await _tasks.GetByIdAsync(t1.Id))!.Status);
        Assert.Equal(TaskStatus.Queued, (await _tasks.GetByIdAsync(t2.Id))!.Status);
    }

    [Fact]
    public async Task BatchTools_RejectEmptyBatch()
    {
        var sut = BuildSut();
        await Assert.ThrowsAsync<InvalidOperationException>(
            () => sut.BatchGetTasks(Array.Empty<string>(), CancellationToken.None));
    }

    [Fact]
    public async Task BatchTools_RejectOversizedBatch()
    {
        var sut = BuildSut();
        var ids = Enumerable.Range(0, 101).Select(i => i.ToString()).ToArray();

        var ex = await Assert.ThrowsAsync<InvalidOperationException>(
            () => sut.BatchGetTasks(ids, CancellationToken.None));
        Assert.Contains("max", ex.Message, StringComparison.OrdinalIgnoreCase);
    }
}
