"""Integration tests for persuade iterate-pr CLI command."""

import json
import subprocess
from pathlib import Path

import pytest


class TestIteratePRCLIIntegration:
    """Integration tests for the iterate-pr CLI flow."""

    @pytest.fixture
    def mock_project(self, tmp_path: Path) -> Path:
        """Create a mock project with PR state file."""
        state_dir = tmp_path / ".claude" / "state"
        state_dir.mkdir(parents=True)

        state_file = state_dir / "pr_999_monitor.json"
        state_file.write_text(
            json.dumps(
                {
                    "pr_number": 999,
                    "feedback_items": [
                        {
                            "id": "test-1",
                            "item_type": "review_comment",
                            "author": "reviewer",
                            "author_type": "human",
                            "body": "Please add tests for this function",
                            "url": "https://github.com/test/repo/pull/999#comment-1",
                            "status": "unaddressed",
                        }
                    ],
                }
            )
        )
        return tmp_path

    def test_plan_phase_reads_state_file(self, mock_project: Path) -> None:
        """Test that plan phase correctly reads and displays state file."""
        # Note: This test requires persuade[agents] to be installed
        # If not installed, it should fail gracefully with ImportError message
        result = subprocess.run(
            ["persuade", "iterate-pr", "999", "--project-dir", str(mock_project)],
            capture_output=True,
            text=True,
            timeout=30,
            input="n\n",  # Decline execution if prompted
        )
        # Should either show plan info or handle missing dependency gracefully
        output = result.stdout + result.stderr
        assert (
            "999" in output
            or "unaddressed" in output.lower()
            or "Missing dependency" in output
            or "iterate-pr" in output
        )

    def test_plan_phase_handles_missing_state(self, tmp_path: Path) -> None:
        """Test plan phase when no state file exists."""
        result = subprocess.run(
            ["persuade", "iterate-pr", "888", "--project-dir", str(tmp_path)],
            capture_output=True,
            text=True,
            timeout=30,
            input="n\n",  # Decline execution if prompted
        )
        # Should handle gracefully (no crash), either with output or missing dep message
        assert result.returncode in [0, 1]

    def test_help_shows_options(self) -> None:
        """Test that --help shows all options."""
        result = subprocess.run(
            ["persuade", "iterate-pr", "--help"],
            capture_output=True,
            text=True,
        )
        assert result.returncode == 0
        assert "--auto-approve" in result.stdout
        assert "--project-dir" in result.stdout
        assert "PR_NUMBER" in result.stdout

    def test_auto_approve_flag_accepted(self) -> None:
        """Test that -y/--auto-approve flag is recognized."""
        # Just verify the flag is accepted (will fail if command not valid)
        result = subprocess.run(
            ["persuade", "iterate-pr", "--help"],
            capture_output=True,
            text=True,
        )
        assert "-y" in result.stdout or "--auto-approve" in result.stdout
