using ClaudeDo.Data;
using ClaudeDo.Data.Git;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Planning;
using ClaudeDo.Worker.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Worker.Tests.Planning;

public class PlanningAggregatorTests : IDisposable
{
    private readonly List<DbFixture> _dbs = new();
    private readonly List<GitRepoFixture> _repos = new();
    private readonly List<(string repoDir, string wtPath)> _wtCleanups = new();

    private DbFixture NewDb() { var d = new DbFixture(); _dbs.Add(d); return d; }
    private GitRepoFixture NewRepo() { var r = new GitRepoFixture(); _repos.Add(r); return r; }

    public void Dispose()
    {
        foreach (var (repo, wt) in _wtCleanups)
            try { GitRepoFixture.RunGit(repo, "worktree", "remove", "--force", wt); } catch { }
        foreach (var d in _dbs) try { d.Dispose(); } catch { }
        foreach (var r in _repos) try { r.Dispose(); } catch { }
    }

    [Fact]
    public async Task GetAggregatedDiffAsync_ReturnsOneEntryPerSubtaskInSortOrder()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");

        var (parentId, subAId, subBId) = await SeedPlanningWithTwoChildrenAsync(db, repo);

        var svc = new PlanningAggregator(
            db.CreateFactory(),
            new GitService(),
            NullLogger<PlanningAggregator>.Instance);

        var result = await svc.GetAggregatedDiffAsync(parentId, CancellationToken.None);

        Assert.Equal(2, result.Count);
        Assert.Equal(subAId, result[0].SubtaskId);
        Assert.Equal(subBId, result[1].SubtaskId);
        Assert.Contains("diff --git", result[0].UnifiedDiff);
        Assert.Contains("diff --git", result[1].UnifiedDiff);
    }

    private async Task<(string parentId, string subA, string subB)> SeedPlanningWithTwoChildrenAsync(
        DbFixture db, GitRepoFixture repo)
    {
        using var ctx = db.CreateContext();

        // List with WorkingDir set to the repo.
        var listId = Guid.NewGuid().ToString();
        ctx.Lists.Add(new ListEntity
        {
            Id = listId, Name = "test", CreatedAt = DateTime.UtcNow,
            WorkingDir = repo.RepoDir,
        });

        // Planning parent.
        var parentId = Guid.NewGuid().ToString();
        ctx.Tasks.Add(new TaskEntity
        {
            Id = parentId, ListId = listId, Title = "plan", CreatedAt = DateTime.UtcNow,
            Status = TaskStatus.Idle, PlanningPhase = PlanningPhase.Active, SortOrder = 0, Number = 1,
        });

        // Two children (sorted A then B).
        var subA = Guid.NewGuid().ToString();
        var subB = Guid.NewGuid().ToString();
        ctx.Tasks.Add(new TaskEntity
        {
            Id = subA, ListId = listId, Title = "child A", CreatedAt = DateTime.UtcNow,
            ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 1, Number = 2,
        });
        ctx.Tasks.Add(new TaskEntity
        {
            Id = subB, ListId = listId, Title = "child B", CreatedAt = DateTime.UtcNow,
            ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 2, Number = 3,
        });
        await ctx.SaveChangesAsync();

        // Create real worktrees for each child with a distinct commit.
        SeedWorktree(ctx, repo, subA, "fileA.txt", "content A");
        SeedWorktree(ctx, repo, subB, "fileB.txt", "content B");
        await ctx.SaveChangesAsync();

        return (parentId, subA, subB);
    }

    private void SeedWorktree(ClaudeDoDbContext ctx, GitRepoFixture repo, string taskId, string filename, string content)
    {
        var wtPath = Path.Combine(Path.GetTempPath(), $"wt_{Guid.NewGuid():N}");
        _wtCleanups.Add((repo.RepoDir, wtPath));
        var branch = $"claudedo/{taskId[..8]}";
        GitRepoFixture.RunGit(repo.RepoDir, "worktree", "add", "-b", branch, wtPath, repo.BaseCommit);
        File.WriteAllText(Path.Combine(wtPath, filename), content);
        GitRepoFixture.RunGit(wtPath, "add", filename);
        GitRepoFixture.RunGit(wtPath, "commit", "-m", $"add {filename}");
        var head = GitRepoFixture.RunGit(wtPath, "rev-parse", "HEAD").Trim();

        ctx.Worktrees.Add(new WorktreeEntity
        {
            TaskId = taskId,
            Path = wtPath,
            BranchName = branch,
            BaseCommit = repo.BaseCommit,
            HeadCommit = head,
            DiffStat = null,
            State = WorktreeState.Active,
            CreatedAt = DateTime.UtcNow,
        });
    }

    [Fact]
    public async Task BuildIntegrationBranchAsync_NonConflictingChildren_ReturnsOkWithCombinedDiff()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");
        var (parentId, _, _) = await SeedPlanningWithTwoChildrenAsync(db, repo);

        var git = new GitService();
        var svc = new PlanningAggregator(db.CreateFactory(), git, NullLogger<PlanningAggregator>.Instance);

        var result = await svc.BuildIntegrationBranchAsync(parentId, targetBranch: "main", CancellationToken.None);

        var ok = Assert.IsType<CombinedDiffResult.Ok>(result);
        Assert.EndsWith("-integration", ok.Value.IntegrationBranch);
        Assert.Contains("diff --git", ok.Value.UnifiedDiff);

        var branches = await git.ListLocalBranchesAsync(repo.RepoDir, CancellationToken.None);
        Assert.Contains(branches, b => b == ok.Value.IntegrationBranch);
    }

    [Fact]
    public async Task BuildIntegrationBranchAsync_ConflictingChildren_ReturnsFailedAndResetsBranch()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");
        var (parentId, subA, subB) = await SeedPlanningWithConflictingChildrenAsync(db, repo);

        var git = new GitService();
        var svc = new PlanningAggregator(db.CreateFactory(), git, NullLogger<PlanningAggregator>.Instance);

        var result = await svc.BuildIntegrationBranchAsync(parentId, targetBranch: "main", CancellationToken.None);

        var failed = Assert.IsType<CombinedDiffResult.Failed>(result);
        Assert.Equal(subB, failed.Value.FirstConflictSubtaskId);
        Assert.NotEmpty(failed.Value.ConflictedFiles);

        Assert.False(await git.IsMidMergeAsync(repo.RepoDir, CancellationToken.None));
    }

    private async Task<(string parentId, string subA, string subB)> SeedPlanningWithConflictingChildrenAsync(
        DbFixture db, GitRepoFixture repo)
    {
        using var ctx = db.CreateContext();
        var listId = Guid.NewGuid().ToString();
        ctx.Lists.Add(new ListEntity
        {
            Id = listId, Name = "test", CreatedAt = DateTime.UtcNow, WorkingDir = repo.RepoDir,
        });
        var parentId = Guid.NewGuid().ToString();
        ctx.Tasks.Add(new TaskEntity
        {
            Id = parentId, ListId = listId, Title = "plan", CreatedAt = DateTime.UtcNow,
            Status = TaskStatus.Idle, PlanningPhase = PlanningPhase.Active, SortOrder = 0, Number = 1,
        });
        var subA = Guid.NewGuid().ToString();
        var subB = Guid.NewGuid().ToString();
        ctx.Tasks.Add(new TaskEntity
        {
            Id = subA, ListId = listId, Title = "A", CreatedAt = DateTime.UtcNow,
            ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 1, Number = 2,
        });
        ctx.Tasks.Add(new TaskEntity
        {
            Id = subB, ListId = listId, Title = "B", CreatedAt = DateTime.UtcNow,
            ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 2, Number = 3,
        });
        await ctx.SaveChangesAsync();

        SeedWorktreeWithFile(ctx, repo, subA, "README.md", "A wins\n");
        SeedWorktreeWithFile(ctx, repo, subB, "README.md", "B wins\n");
        await ctx.SaveChangesAsync();

        return (parentId, subA, subB);
    }

    [Fact]
    public async Task CleanupIntegrationBranchAsync_RemovesBranchIfPresent()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");
        var (parentId, _, _) = await SeedPlanningWithTwoChildrenAsync(db, repo);

        var git = new GitService();
        var svc = new PlanningAggregator(db.CreateFactory(), git, NullLogger<PlanningAggregator>.Instance);

        var built = await svc.BuildIntegrationBranchAsync(parentId, "main", CancellationToken.None);
        var ok = Assert.IsType<CombinedDiffResult.Ok>(built);

        await svc.CleanupIntegrationBranchAsync(parentId, CancellationToken.None);

        var branches = await git.ListLocalBranchesAsync(repo.RepoDir, CancellationToken.None);
        Assert.DoesNotContain(branches, b => b == ok.Value.IntegrationBranch);
    }

    private void SeedWorktreeWithFile(ClaudeDoDbContext ctx, GitRepoFixture repo, string taskId, string filename, string content)
    {
        var wtPath = Path.Combine(Path.GetTempPath(), $"wt_{Guid.NewGuid():N}");
        _wtCleanups.Add((repo.RepoDir, wtPath));
        var branch = $"claudedo/{taskId[..8]}";
        GitRepoFixture.RunGit(repo.RepoDir, "worktree", "add", "-b", branch, wtPath, repo.BaseCommit);
        File.WriteAllText(Path.Combine(wtPath, filename), content);
        GitRepoFixture.RunGit(wtPath, "add", filename);
        GitRepoFixture.RunGit(wtPath, "commit", "-m", $"edit {filename}");
        var head = GitRepoFixture.RunGit(wtPath, "rev-parse", "HEAD").Trim();
        ctx.Worktrees.Add(new WorktreeEntity
        {
            TaskId = taskId, Path = wtPath, BranchName = branch,
            BaseCommit = repo.BaseCommit, HeadCommit = head,
            DiffStat = null, State = WorktreeState.Active, CreatedAt = DateTime.UtcNow,
        });
    }
}
