"""Unit tests for admin test-plan-implement-gh-workflow command."""

from click.testing import CliRunner

from erk.cli.cli import cli
from erk_shared.gateway.git.fake import FakeGit
from erk_shared.gateway.github.fake import FakeGitHub
from erk_shared.gateway.github.issues.fake import FakeGitHubIssues
from tests.test_utils.env_helpers import erk_isolated_fs_env


def test_happy_path_with_existing_issue() -> None:
    """Command succeeds with --plan flag, using existing plan number."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner, env_overrides=None) as env:
        fake_github = FakeGitHub()
        fake_issues = FakeGitHubIssues()
        ctx = env.build_context(
            current_branch="my-feature",
            github=fake_github,
            issues=fake_issues,
        )

        result = runner.invoke(
            cli, ["admin", "test-plan-implement-gh-workflow", "--plan", "42"], obj=ctx
        )

        assert result.exit_code == 0, f"Command failed: {result.output}"
        # Verify plan 42 was used
        assert "Using existing plan #42" in result.output
        # Verify PR was created as draft
        assert len(fake_github.created_prs) == 1
        branch, title, _body, base, draft = fake_github.created_prs[0]
        assert branch.startswith("test-workflow-")
        assert base == "main"
        assert draft is True
        # Verify workflow was triggered
        assert len(fake_github.triggered_workflows) == 1
        workflow, inputs, _ref = fake_github.triggered_workflows[0]
        assert workflow == "plan-implement.yml"
        assert inputs["plan_id"] == "42"
        # Verify output contains run URL
        assert "Workflow dispatched successfully" in result.output
        assert "Run URL:" in result.output


def test_happy_path_creating_new_issue() -> None:
    """Command succeeds without --plan, creating a new plan."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner, env_overrides=None) as env:
        fake_github = FakeGitHub()
        fake_issues = FakeGitHubIssues()
        ctx = env.build_context(
            current_branch="my-feature",
            github=fake_github,
            issues=fake_issues,
        )

        result = runner.invoke(cli, ["admin", "test-plan-implement-gh-workflow"], obj=ctx)

        assert result.exit_code == 0, f"Command failed: {result.output}"
        # Verify issue was created
        assert len(fake_issues.created_issues) == 1
        title, _body, labels = fake_issues.created_issues[0]
        assert title == "Test workflow run"
        assert "test" in labels
        assert "Created test plan #1" in result.output
        # Verify workflow was triggered with the new plan number
        assert len(fake_github.triggered_workflows) == 1
        _, inputs, _ref = fake_github.triggered_workflows[0]
        assert inputs["plan_id"] == "1"


def test_happy_path_uses_detected_trunk_branch() -> None:
    """Command uses detected trunk branch (non-main) as PR base and workflow base_branch."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner, env_overrides=None) as env:
        fake_github = FakeGitHub()
        fake_issues = FakeGitHubIssues()
        git = FakeGit(
            git_common_dirs={env.cwd: env.git_dir},
            current_branches={env.cwd: "my-feature"},
            existing_paths={env.cwd, env.git_dir},
            remote_urls={(env.cwd, "origin"): "https://github.com/owner/repo.git"},
            trunk_branches={env.cwd: "master"},
        )
        ctx = env.build_context(
            current_branch="my-feature",
            git=git,
            github=fake_github,
            issues=fake_issues,
        )

        result = runner.invoke(
            cli, ["admin", "test-plan-implement-gh-workflow", "--plan", "42"], obj=ctx
        )

        assert result.exit_code == 0, f"Command failed: {result.output}"
        assert len(fake_github.created_prs) == 1
        _branch, _title, _body, base, _draft = fake_github.created_prs[0]
        assert base == "master"
        assert len(fake_github.triggered_workflows) == 1
        _workflow, inputs, _ref = fake_github.triggered_workflows[0]
        assert inputs["base_branch"] == "master"


def test_error_no_github_remote() -> None:
    """Command fails with clear error when repo has no GitHub remote."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner, env_overrides=None) as env:
        git = FakeGit(
            git_common_dirs={env.cwd: env.git_dir},
            current_branches={env.cwd: "my-feature"},
            existing_paths={env.cwd, env.git_dir},
            remote_urls={},
        )
        ctx = env.build_context(git=git)

        result = runner.invoke(cli, ["admin", "test-plan-implement-gh-workflow"], obj=ctx)

        assert result.exit_code == 1
        assert "Not a GitHub repository" in result.output


def test_error_detached_head() -> None:
    """Command fails with clear error when in detached HEAD state."""
    runner = CliRunner()
    with erk_isolated_fs_env(runner, env_overrides=None) as env:
        # Don't set current_branch so get_current_branch returns None
        ctx = env.build_context()

        result = runner.invoke(cli, ["admin", "test-plan-implement-gh-workflow"], obj=ctx)

        assert result.exit_code == 1
        assert "detached HEAD" in result.output
