using ClaudeDo.Data.Git;
using ClaudeDo.Worker.Tests.Infrastructure;

namespace ClaudeDo.Worker.Tests.Runner;

public class GitServiceMergeTests : IDisposable
{
    private readonly List<GitRepoFixture> _repos = new();

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

    public void Dispose()
    {
        foreach (var r in _repos) try { r.Dispose(); } catch { }
    }

    [Fact]
    public async Task GetCurrentBranchAsync_FreshRepo_ReturnsDefaultBranch()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;

        var repo = NewRepo();
        var git = new GitService();

        var branch = await git.GetCurrentBranchAsync(repo.RepoDir);

        Assert.False(string.IsNullOrWhiteSpace(branch));
        // Default branch is either "main" or "master" depending on git config.
        Assert.True(branch == "main" || branch == "master",
            $"Expected main or master, got '{branch}'");
    }

    [Fact]
    public async Task ListLocalBranchesAsync_AfterCreatingSecondBranch_ReturnsBoth()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;

        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "feature/x");

        var git = new GitService();
        var branches = await git.ListLocalBranchesAsync(repo.RepoDir);

        Assert.Contains("feature/x", branches);
        Assert.Contains(branches, b => b == "main" || b == "master");
    }

    [Fact]
    public async Task IsMidMergeAsync_FreshRepo_ReturnsFalse()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;
        var repo = NewRepo();
        var git = new GitService();

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

    [Fact]
    public async Task IsMidMergeAsync_MergeHeadPresent_ReturnsTrue()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;
        var repo = NewRepo();
        // Simulate a mid-merge state by dropping a MERGE_HEAD file.
        var mergeHead = Path.Combine(repo.RepoDir, ".git", "MERGE_HEAD");
        File.WriteAllText(mergeHead, "0000000000000000000000000000000000000000\n");

        var git = new GitService();
        Assert.True(await git.IsMidMergeAsync(repo.RepoDir));
    }

    [Fact]
    public async Task MergeNoFfAsync_DivergedNonConflicting_ReturnsZero_AndCreatesMergeCommit()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;
        var repo = NewRepo();

        // Create a feature branch with one new file.
        GitRepoFixture.RunGit(repo.RepoDir, "checkout", "-b", "feature/merge");
        File.WriteAllText(Path.Combine(repo.RepoDir, "feature.txt"), "hello\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "feat: add feature.txt");

        // Back to default and add a non-overlapping file so history diverges.
        string defaultBranch;
        try { defaultBranch = GitRepoFixture.RunGit(repo.RepoDir, "symbolic-ref", "--short", "refs/remotes/origin/HEAD").Trim().Replace("origin/", ""); }
        catch { defaultBranch = "main"; }
        if (string.IsNullOrEmpty(defaultBranch)) defaultBranch = "main";
        try { GitRepoFixture.RunGit(repo.RepoDir, "checkout", defaultBranch); }
        catch { GitRepoFixture.RunGit(repo.RepoDir, "checkout", "master"); defaultBranch = "master"; }
        File.WriteAllText(Path.Combine(repo.RepoDir, "other.txt"), "other\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "chore: add other.txt");

        var git = new GitService();
        var (exitCode, stderr) = await git.MergeNoFfAsync(repo.RepoDir, "feature/merge", "Merge feature/merge");

        Assert.Equal(0, exitCode);
        // Confirm merge commit exists (two parents on HEAD).
        var parents = GitRepoFixture.RunGit(repo.RepoDir, "rev-list", "--parents", "-n", "1", "HEAD").Trim();
        Assert.True(parents.Split(' ').Length >= 3, $"Expected merge commit (3 tokens), got: '{parents}'");
    }

    [Fact]
    public async Task MergeNoFfAsync_Conflict_ReturnsNonZero()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;
        var repo = NewRepo();

        // Both branches modify README.md — guaranteed conflict.
        GitRepoFixture.RunGit(repo.RepoDir, "checkout", "-b", "feature/conflict");
        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "# feature side\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "feat: feature edit");

        try { GitRepoFixture.RunGit(repo.RepoDir, "checkout", "main"); }
        catch { GitRepoFixture.RunGit(repo.RepoDir, "checkout", "master"); }
        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "# main side\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "chore: main edit");

        var git = new GitService();
        var (exitCode, _) = await git.MergeNoFfAsync(repo.RepoDir, "feature/conflict", "merge");

        Assert.NotEqual(0, exitCode);
    }

    [Fact]
    public async Task MergeAbortAsync_AfterConflict_ClearsMergeState()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;
        var repo = NewRepo();

        GitRepoFixture.RunGit(repo.RepoDir, "checkout", "-b", "feature/abort");
        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "# feat side\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "edit");

        try { GitRepoFixture.RunGit(repo.RepoDir, "checkout", "main"); }
        catch { GitRepoFixture.RunGit(repo.RepoDir, "checkout", "master"); }
        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "# main side\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "edit");

        var git = new GitService();
        await git.MergeNoFfAsync(repo.RepoDir, "feature/abort", "merge"); // will conflict

        Assert.True(await git.IsMidMergeAsync(repo.RepoDir));
        await git.MergeAbortAsync(repo.RepoDir);
        Assert.False(await git.IsMidMergeAsync(repo.RepoDir));
    }

    [Fact]
    public async Task CheckoutBranchAsync_ExistingBranch_SwitchesBranch()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;
        var repo = NewRepo();
        GitRepoFixture.RunGit(repo.RepoDir, "branch", "feature/checkout-test");

        var git = new GitService();
        await git.CheckoutBranchAsync(repo.RepoDir, "feature/checkout-test");

        var current = await git.GetCurrentBranchAsync(repo.RepoDir);
        Assert.Equal("feature/checkout-test", current);
    }

    [Fact]
    public async Task ListConflictedFilesAsync_MidConflict_ReturnsConflictedFile()
    {
        if (!GitRepoFixture.IsGitAvailable()) return;
        var repo = NewRepo();

        GitRepoFixture.RunGit(repo.RepoDir, "checkout", "-b", "feature/cflist");
        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "# feat\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "edit");

        try { GitRepoFixture.RunGit(repo.RepoDir, "checkout", "main"); }
        catch { GitRepoFixture.RunGit(repo.RepoDir, "checkout", "master"); }
        File.WriteAllText(Path.Combine(repo.RepoDir, "README.md"), "# main\n");
        GitRepoFixture.RunGit(repo.RepoDir, "add", "-A");
        GitRepoFixture.RunGit(repo.RepoDir, "commit", "-m", "edit");

        var git = new GitService();
        await git.MergeNoFfAsync(repo.RepoDir, "feature/cflist", "merge");

        var files = await git.ListConflictedFilesAsync(repo.RepoDir);
        Assert.Contains("README.md", files);

        await git.MergeAbortAsync(repo.RepoDir);
    }
}
