using ClaudeDo.Data;
using ClaudeDo.Data.Git;
using ClaudeDo.Data.Models;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Lifecycle;
using ClaudeDo.Worker.Planning;
using ClaudeDo.Worker.Tests.Infrastructure;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Worker.Tests.Planning;

file sealed class OrchestratorRecordingHubClients : IHubClients
{
    public OrchestratorRecordingClientProxy Proxy { get; } = new();
    public IClientProxy All => Proxy;
    public IClientProxy AllExcept(IReadOnlyList<string> excludedConnectionIds) => Proxy;
    public IClientProxy Client(string connectionId) => Proxy;
    public IClientProxy Clients(IReadOnlyList<string> connectionIds) => Proxy;
    public IClientProxy Group(string groupName) => Proxy;
    public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludedConnectionIds) => Proxy;
    public IClientProxy Groups(IReadOnlyList<string> groupNames) => Proxy;
    public IClientProxy User(string userId) => Proxy;
    public IClientProxy Users(IReadOnlyList<string> userIds) => Proxy;
}

file sealed class OrchestratorRecordingClientProxy : IClientProxy
{
    public List<(string Method, object?[] Args)> Calls { get; } = new();
    public Task SendCoreAsync(string method, object?[] args, CancellationToken cancellationToken = default)
    {
        Calls.Add((method, args));
        return Task.CompletedTask;
    }
}

file sealed class OrchestratorFakeHubContext : IHubContext<WorkerHub>
{
    public OrchestratorRecordingHubClients RecordingClients { get; } = new();
    public IHubClients Clients => RecordingClients;
    public IGroupManager Groups => throw new NotImplementedException();
}

public sealed class PlanningMergeOrchestratorTests : 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 StartAsync_AllChildrenMergeCleanly_MarksPlanningDoneAndEmitsCompleted()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");

        var (parentId, subA, subB) = await SeedPlanningWithTwoNonConflictingChildrenAsync(db, repo);

        var (orch, calls) = BuildOrchestrator(db);

        await orch.StartAsync(parentId, "main", CancellationToken.None);

        using var ctx = db.CreateContext();
        var planning = ctx.Tasks.Single(t => t.Id == parentId);
        Assert.Equal(TaskStatus.Done, planning.Status);
        Assert.NotNull(planning.FinishedAt);

        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subA).State);
        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subB).State);

        Assert.Contains(calls, c => c.Method == "PlanningMergeStarted");
        Assert.Equal(2, calls.Count(c => c.Method == "PlanningSubtaskMerged"));
        Assert.Contains(calls, c => c.Method == "PlanningCompleted" && (string)c.Args[0]! == parentId);
    }

    private async Task<(string parentId, string subA, string subB)> SeedPlanningWithTwoNonConflictingChildrenAsync(
        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.WaitingForReview, PlanningPhase = PlanningPhase.Finalized, SortOrder = 0,
        });

        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,
        });
        ctx.Tasks.Add(new TaskEntity
        {
            Id = subB, ListId = listId, Title = "child B", CreatedAt = DateTime.UtcNow,
            ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 2,
        });
        await ctx.SaveChangesAsync();

        SeedWorktree(ctx, repo, subA, "fileA.txt", "content A");
        SeedWorktree(ctx, repo, subB, "fileB.txt", "content B");
        await ctx.SaveChangesAsync();

        return (parentId, subA, subB);
    }

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

        var (parentId, subA, subB, subC) = await SeedPlanningThreeChildrenMiddleConflictsAsync(db, repo);

        var (orch, spy) = BuildOrchestrator(db);
        await orch.StartAsync(parentId, "main", CancellationToken.None);

        Assert.Contains(spy, c => c.Method == "PlanningSubtaskMerged" && (string)c.Args[1]! == subA);
        Assert.Contains(spy, c => c.Method == "PlanningMergeConflict" && (string)c.Args[1]! == subB);

        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "resolved\n");

        await orch.ContinueAsync(parentId, CancellationToken.None);

        using var ctx = db.CreateContext();
        Assert.Equal(TaskStatus.Done, ctx.Tasks.Single(t => t.Id == parentId).Status);
        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subB).State);
        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subC).State);
        Assert.Contains(spy, c => c.Method == "PlanningSubtaskMerged" && (string)c.Args[1]! == subB);
        Assert.Contains(spy, c => c.Method == "PlanningSubtaskMerged" && (string)c.Args[1]! == subC);
        Assert.Contains(spy, c => c.Method == "PlanningCompleted");
    }

    private async Task<(string parentId, string subA, string subB, string subC)> SeedPlanningThreeChildrenMiddleConflictsAsync(
        DbFixture db, GitRepoFixture repo)
    {
        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "# main change\n");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-am", "main change README");

        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.WaitingForReview, PlanningPhase = PlanningPhase.Finalized, SortOrder = 0,
        });
        var subA = Guid.NewGuid().ToString();
        var subB = Guid.NewGuid().ToString();
        var subC = Guid.NewGuid().ToString();
        ctx.Tasks.AddRange(
            new TaskEntity { Id = subA, ListId = listId, Title = "A", CreatedAt = DateTime.UtcNow, ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 1 },
            new TaskEntity { Id = subB, ListId = listId, Title = "B", CreatedAt = DateTime.UtcNow, ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 2 },
            new TaskEntity { Id = subC, ListId = listId, Title = "C", CreatedAt = DateTime.UtcNow, ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 3 }
        );
        await ctx.SaveChangesAsync();

        SeedWorktreeWithFile(ctx, repo, subA, "fileA.txt", "A\n");
        SeedWorktreeWithFile(ctx, repo, subB, "README.md", "branch change\n");
        SeedWorktreeWithFile(ctx, repo, subC, "fileC.txt", "C\n");
        await ctx.SaveChangesAsync();

        return (parentId, subA, subB, subC);
    }

    private void SeedWorktreeWithFile(ClaudeDoDbContext ctx, GitRepoFixture repo, string taskId, string filename, string content)
        => SeedWorktree(ctx, repo, taskId, filename, content);

    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 AbortAsync_AfterConflict_RestoresCleanRepoAndClearsState()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");

        var (parentId, subA, subB, _) = await SeedPlanningThreeChildrenMiddleConflictsAsync(db, repo);

        var (orch, spy) = BuildOrchestrator(db);
        await orch.StartAsync(parentId, "main", CancellationToken.None);

        await orch.AbortAsync(parentId, CancellationToken.None);

        using var ctx = db.CreateContext();
        // Planning stays in Planned — NOT flipped to Done.
        Assert.Equal(PlanningPhase.Finalized, ctx.Tasks.Single(t => t.Id == parentId).PlanningPhase);
        // Earlier successful merge stays merged.
        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subA).State);
        // Conflicted subtask's worktree stays Active (abort doesn't flip it).
        Assert.Equal(WorktreeState.Active, ctx.Worktrees.Single(w => w.TaskId == subB).State);

        Assert.Contains(spy, c => c.Method == "PlanningMergeAborted" && (string)c.Args[0]! == parentId);

        // Repo no longer mid-merge.
        var git = new GitService();
        Assert.False(await git.IsMidMergeAsync(repo.RepoDir, CancellationToken.None));
    }

    // ─── Stateless abort (post-restart recovery) ───────────────────────────

    /// <summary>
    /// Worker restarted while a conflict was paused: _states is empty but the list repo is
    /// still mid-merge. AbortAsync must abort the dangling merge, broadcast PlanningMergeAborted,
    /// and leave the parent in WaitingForReview so a fresh Approve can retry.
    /// </summary>
    [Fact]
    public async Task AbortAsync_NoState_RepoMidMerge_AbortsAndBroadcasts()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");

        var (parentId, _, subB, _) = await SeedPlanningThreeChildrenMiddleConflictsAsync(db, repo);

        // Drive orch1 into the conflict pause — repo is now mid-merge.
        var (orch1, _) = BuildOrchestrator(db);
        await orch1.StartAsync(parentId, "main", CancellationToken.None);

        // Simulate restart: fresh orchestrator has no in-memory state.
        var (orch2, spy) = BuildOrchestrator(db);

        await orch2.AbortAsync(parentId, CancellationToken.None);

        var git = new GitService();
        Assert.False(await git.IsMidMergeAsync(repo.RepoDir, CancellationToken.None));

        using var ctx = db.CreateContext();
        Assert.Equal(TaskStatus.WaitingForReview, ctx.Tasks.Single(t => t.Id == parentId).Status);

        Assert.Contains(spy, c => c.Method == "PlanningMergeAborted" && (string)c.Args[0]! == parentId);
    }

    /// <summary>
    /// No in-memory state and repo is clean — nothing to abort. Must throw a clear error.
    /// </summary>
    [Fact]
    public async Task AbortAsync_NoState_RepoNotMidMerge_ThrowsClear()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");

        var (parentId, _, _) = await SeedPlanningWithTwoNonConflictingChildrenAsync(db, repo);

        var (orch, _) = BuildOrchestrator(db);

        var ex = await Assert.ThrowsAsync<InvalidOperationException>(
            () => orch.AbortAsync(parentId, CancellationToken.None));
        Assert.Contains("no in-progress merge", ex.Message);
    }

    private (PlanningMergeOrchestrator orch, List<(string Method, object?[] Args)> calls) BuildOrchestrator(DbFixture db)
    {
        var fakeHub = new OrchestratorFakeHubContext();
        var spy = fakeHub.RecordingClients.Proxy;
        var broadcaster = new HubBroadcaster(fakeHub);
        var git = new GitService();
        var factory = db.CreateFactory();
        var built = TaskStateServiceBuilder.Build(factory);
        var merge = new TaskMergeService(
            factory, git, broadcaster,
            built.State,
            NullLogger<TaskMergeService>.Instance);
        var aggregator = new PlanningAggregator(
            factory, git,
            NullLogger<PlanningAggregator>.Instance);
        var orch = new PlanningMergeOrchestrator(
            factory, merge, aggregator, broadcaster, git,
            built.State,
            NullLogger<PlanningMergeOrchestrator>.Instance);
        return (orch, spy.Calls);
    }

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

        var (parentId, runningSub) = await SeedPlanningWithOneRunningChildAsync(db, repo);

        var (orch, spy) = BuildOrchestrator(db);

        var ex = await Assert.ThrowsAsync<InvalidOperationException>(
            () => orch.StartAsync(parentId, "main", CancellationToken.None));
        Assert.Contains(runningSub, ex.Message);

        using var ctx = db.CreateContext();
        Assert.Equal(PlanningPhase.Finalized, ctx.Tasks.Single(t => t.Id == parentId).PlanningPhase);
        Assert.Empty(spy);
    }

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

        File.WriteAllText(Path.Combine(repo.RepoDir, "dirty.txt"), "unstaged\n");

        var (orch, _) = BuildOrchestrator(db);

        var ex = await Assert.ThrowsAsync<InvalidOperationException>(
            () => orch.StartAsync(parentId, "main", CancellationToken.None));
        Assert.Contains("uncommitted", ex.Message);
    }

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

        var (parentId, subA, subB) = await SeedPlanningWithTwoNonConflictingChildrenAsync(db, repo);
        using (var setup = db.CreateContext())
        {
            var wt = setup.Worktrees.Single(w => w.TaskId == subA);
            wt.State = WorktreeState.Merged;
            await setup.SaveChangesAsync();
        }

        var (orch, spy) = BuildOrchestrator(db);
        await orch.StartAsync(parentId, "main", CancellationToken.None);

        Assert.DoesNotContain(spy, c => c.Method == "PlanningSubtaskMerged" && (string)c.Args[1]! == subA);
        Assert.Contains(spy, c => c.Method == "PlanningSubtaskMerged" && (string)c.Args[1]! == subB);
        Assert.Contains(spy, c => c.Method == "PlanningCompleted");
    }

    private async Task<(string parentId, string runningChild)> SeedPlanningWithOneRunningChildAsync(
        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.Finalized, SortOrder = 0,
        });
        var running = Guid.NewGuid().ToString();
        ctx.Tasks.Add(new TaskEntity
        {
            Id = running, ListId = listId, Title = "still running",
            CreatedAt = DateTime.UtcNow, ParentTaskId = parentId,
            Status = TaskStatus.Running, SortOrder = 1,
        });
        await ctx.SaveChangesAsync();
        SeedWorktreeWithFile(ctx, repo, running, "fileR.txt", "R\n");
        await ctx.SaveChangesAsync();
        return (parentId, running);
    }

    // ─── ApproveReview routing ──────────────────────────────────────────────

    /// <summary>
    /// Improvement parent (PlanningPhase.None) in WaitingForReview with two Done children
    /// that each have an Active worktree → orchestrator merges both and marks the parent Done.
    /// This mirrors the ApproveReview hub path for a parent-with-children.
    /// </summary>
    [Fact]
    public async Task StartAsync_ImprovementParentInWaitingForReview_MergesBothChildrenAndLeavesDone()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");

        var (parentId, subA, subB) = await SeedImprovementParentWithTwoDoneChildrenAsync(db, repo);

        var (orch, calls) = BuildOrchestrator(db);

        await orch.StartAsync(parentId, "main", CancellationToken.None);

        using var ctx = db.CreateContext();
        var parent = ctx.Tasks.Single(t => t.Id == parentId);
        Assert.Equal(TaskStatus.Done, parent.Status);
        Assert.NotNull(parent.FinishedAt);

        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subA).State);
        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subB).State);

        Assert.Contains(calls, c => c.Method == "PlanningMergeStarted");
        Assert.Equal(2, calls.Count(c => c.Method == "PlanningSubtaskMerged"));
        Assert.Contains(calls, c => c.Method == "PlanningCompleted" && (string)c.Args[0]! == parentId);
    }

    private async Task<(string parentId, string subA, string subB)> SeedImprovementParentWithTwoDoneChildrenAsync(
        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();
        // Improvement parent: PlanningPhase.None, status WaitingForReview (after children finished)
        ctx.Tasks.Add(new TaskEntity
        {
            Id = parentId, ListId = listId, Title = "improve", CreatedAt = DateTime.UtcNow,
            Status = TaskStatus.WaitingForReview, PlanningPhase = PlanningPhase.None, SortOrder = 0,
        });

        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,
        });
        ctx.Tasks.Add(new TaskEntity
        {
            Id = subB, ListId = listId, Title = "child B", CreatedAt = DateTime.UtcNow,
            ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 2,
        });
        await ctx.SaveChangesAsync();

        SeedWorktree(ctx, repo, subA, "fileA.txt", "content A");
        SeedWorktree(ctx, repo, subB, "fileB.txt", "content B");
        await ctx.SaveChangesAsync();

        return (parentId, subA, subB);
    }

    /// <summary>
    /// Parent is Cancelled before the orchestrator finalizes (simulates a race where the user
    /// cancels the parent while the merge drain is in progress). After the drain completes,
    /// ApproveReviewAsync sees Status != WaitingForReview and refuses — parent must stay
    /// Cancelled and PlanningCompleted must not be broadcast.
    /// </summary>
    [Fact]
    public async Task StartAsync_ParentCancelledBeforeFinalize_StatusRemainsAndNoPlanningCompleted()
    {
        var db = NewDb();
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");

        // Improvement parent (PlanningPhase.None) seeded as Cancelled — simulates the race
        // where a user or another thread cancelled the parent during the merge drain.
        var (parentId, subA, subB) = await SeedCancelledParentWithDoneChildrenAsync(db, repo);

        var (orch, calls) = BuildOrchestrator(db);
        await orch.StartAsync(parentId, "main", CancellationToken.None);

        using var ctx = db.CreateContext();
        Assert.Equal(TaskStatus.Cancelled, ctx.Tasks.Single(t => t.Id == parentId).Status);
        Assert.DoesNotContain(calls, c => c.Method == "PlanningCompleted");
        // Child worktrees were still merged during the drain
        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subA).State);
        Assert.Equal(WorktreeState.Merged, ctx.Worktrees.Single(w => w.TaskId == subB).State);
    }

    private async Task<(string parentId, string subA, string subB)> SeedCancelledParentWithDoneChildrenAsync(
        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 = "improve", CreatedAt = DateTime.UtcNow,
            Status = TaskStatus.Cancelled, PlanningPhase = PlanningPhase.None, SortOrder = 0,
        });

        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,
        });
        ctx.Tasks.Add(new TaskEntity
        {
            Id = subB, ListId = listId, Title = "child B", CreatedAt = DateTime.UtcNow,
            ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 2,
        });
        await ctx.SaveChangesAsync();

        SeedWorktree(ctx, repo, subA, "fileA.txt", "content A");
        SeedWorktree(ctx, repo, subB, "fileB.txt", "content B");
        await ctx.SaveChangesAsync();

        return (parentId, subA, subB);
    }
}
