using ClaudeDo.Worker.Runner;

namespace ClaudeDo.Worker.Tests.Runner;

public sealed class ClaudeArgsBuilderTests
{
    private readonly ClaudeArgsBuilder _builder = new();

    [Fact]
    public void Default_Config_Produces_Base_Args()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, null, null, null));
        Assert.Contains("-p", args);
        Assert.Contains("--output-format stream-json", args);
        Assert.Contains("--verbose", args);
        Assert.Contains("--dangerously-skip-permissions", args);
        Assert.Contains("--json-schema", args);
        Assert.DoesNotContain("--model", args);
        Assert.DoesNotContain("--append-system-prompt", args);
        Assert.DoesNotContain("--agents", args);
        Assert.DoesNotContain("--resume", args);
    }

    [Fact]
    public void Model_Adds_Model_Flag()
    {
        var args = _builder.Build(new ClaudeRunConfig("sonnet-4-6", null, null, null));
        Assert.Contains("--model sonnet-4-6", args);
    }

    [Fact]
    public void SystemPrompt_Adds_Append_System_Prompt_Flag()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, "Be concise.", null, null));
        Assert.Contains("--append-system-prompt", args);
        Assert.Contains("Be concise.", args);
    }

    [Fact]
    public void AgentPath_Adds_Agents_Flag_As_Json()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, null, "/path/to/agent.md", null));
        Assert.Contains("--agents", args);
        Assert.Contains("/path/to/agent.md", args);
    }

    [Fact]
    public void ResumeSessionId_Adds_Resume_Flag()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, null, null, "sess-abc-123"));
        Assert.Contains("--resume sess-abc-123", args);
    }

    [Fact]
    public void All_Options_Set_Includes_All_Flags()
    {
        var args = _builder.Build(new ClaudeRunConfig("opus-4-6", "Be thorough.", "/agents/dev.md", "sess-xyz"));
        Assert.Contains("--model opus-4-6", args);
        Assert.Contains("--append-system-prompt", args);
        Assert.Contains("--agents", args);
        Assert.Contains("--resume sess-xyz", args);
        Assert.Contains("--json-schema", args);
    }

    [Fact]
    public void SystemPrompt_With_Quotes_Is_Escaped()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, """Don't say "hello".""", null, null));
        Assert.Contains("--append-system-prompt", args);
    }

    [Fact]
    public void Build_quotes_system_prompt_with_newline()
    {
        var args = _builder.Build(new ClaudeRunConfig(
            Model: null,
            SystemPrompt: "line1\nline2",
            AgentPath: null,
            ResumeSessionId: null));

        Assert.Contains("--append-system-prompt \"line1\\nline2\"", args);
    }

    [Fact]
    public void Build_quotes_system_prompt_with_tab()
    {
        var args = _builder.Build(new ClaudeRunConfig(
            Model: null,
            SystemPrompt: "col1\tcol2",
            AgentPath: null,
            ResumeSessionId: null));

        Assert.Contains("\"col1", args);
    }

    [Fact]
    public void MaxTurns_Adds_Flag_When_Positive()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, null, null, null, MaxTurns: 25));
        Assert.Contains("--max-turns 25", args);
    }

    [Fact]
    public void MaxTurns_Omitted_When_Null_Or_ZeroOrLess()
    {
        var a = _builder.Build(new ClaudeRunConfig(null, null, null, null, MaxTurns: null));
        var b = _builder.Build(new ClaudeRunConfig(null, null, null, null, MaxTurns: 0));
        Assert.DoesNotContain("--max-turns", a);
        Assert.DoesNotContain("--max-turns", b);
    }

    [Fact]
    public void PermissionMode_bypass_Keeps_DangerousFlag()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, null, null, null, PermissionMode: "bypassPermissions"));
        Assert.Contains("--dangerously-skip-permissions", args);
        Assert.DoesNotContain("--permission-mode", args);
    }

    [Fact]
    public void PermissionMode_acceptEdits_Emits_PermissionMode_Flag()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, null, null, null, PermissionMode: "acceptEdits"));
        Assert.Contains("--permission-mode acceptEdits", args);
        Assert.DoesNotContain("--dangerously-skip-permissions", args);
    }

    [Fact]
    public void PermissionMode_Null_Defaults_To_BypassPermissions()
    {
        var args = _builder.Build(new ClaudeRunConfig(null, null, null, null));
        Assert.Contains("--dangerously-skip-permissions", args);
    }
}

public sealed class MergeInstructionsTests
{
    [Fact]
    public void All_Empty_Returns_Empty()
    {
        var s = ClaudeDo.Worker.Runner.TaskRunner.MergeInstructions(null, null, null);
        Assert.Equal("", s);
    }

    [Fact]
    public void Only_Global_Returns_Global()
    {
        var s = ClaudeDo.Worker.Runner.TaskRunner.MergeInstructions("global rule", null, null);
        Assert.Equal("global rule", s);
    }

    [Fact]
    public void Only_Task_Returns_Task()
    {
        var s = ClaudeDo.Worker.Runner.TaskRunner.MergeInstructions(null, null, "task rule");
        Assert.Equal("task rule", s);
    }

    [Fact]
    public void Global_And_Task_Are_Prepended_With_Separator()
    {
        var s = ClaudeDo.Worker.Runner.TaskRunner.MergeInstructions("global rule", null, "task rule");
        Assert.Equal("global rule\n\ntask rule", s);
    }

    [Fact]
    public void All_Three_Are_Joined_In_Order()
    {
        var s = ClaudeDo.Worker.Runner.TaskRunner.MergeInstructions("G", "L", "T");
        Assert.Equal("G\n\nL\n\nT", s);
    }

    [Fact]
    public void Whitespace_Only_Parts_Are_Skipped()
    {
        var s = ClaudeDo.Worker.Runner.TaskRunner.MergeInstructions("   ", "L", "\t\n");
        Assert.Equal("L", s);
    }
}
