#!/usr/bin/env python3
"""
Test that --resume flag is actually passed through to Claude CLI.

WHY: This test simulates what happens when a user runs claude-mpm --resume
and verifies that the flag makes it all the way through to the Claude command.
"""

import subprocess
import sys
from pathlib import Path

import pytest

project_root = Path(__file__).parent.parent


@pytest.mark.skip(
    reason="Subprocess test that times out in full suite context (>15s); "
    "Test 2 (run --resume -- --model opus) launches claude_mpm.cli which tries to "
    "start the Claude process and can hang. Run standalone: "
    "'python tests/test_resume_actual.py' (~4s)."
)
def test_resume_passthrough():
    """Test that --resume is passed through to the final command."""
    print("Testing --resume flag passthrough...")
    print("-" * 40)

    # Test 1: Simple --resume
    result = subprocess.run(
        [sys.executable, "-m", "claude_mpm.cli", "--resume", "--help"],
        cwd=project_root,
        capture_output=True,
        text=True,
        env={"PYTHONPATH": str(project_root / "src")},
        check=False,
    )

    if result.returncode == 0:
        print("✅ claude-mpm --resume --help executes without error")
    else:
        print("❌ claude-mpm --resume --help failed")
        print(f"   Error: {result.stderr}")
        return False

    # Test 2: With run command and additional Claude args
    result = subprocess.run(
        [
            sys.executable,
            "-m",
            "claude_mpm.cli",
            "run",
            "--resume",
            "--",
            "--model",
            "opus",
        ],
        cwd=project_root,
        capture_output=True,
        text=True,
        env={"PYTHONPATH": str(project_root / "src")},
        check=False,
    )

    # This will fail to launch Claude (which is expected in test) but should parse correctly
    # We check for specific error messages that indicate the args were parsed
    if "Claude CLI" in result.stderr or result.returncode != 0:
        # Expected - we can't actually launch Claude in test, but args parsed
        print("✅ claude-mpm run --resume -- --model opus parses correctly")
    else:
        print("❌ claude-mpm run --resume -- --model opus failed to parse")
        print(f"   Error: {result.stderr}")
        return False

    # Test 3: With run command explicitly
    result = subprocess.run(
        [sys.executable, "-m", "claude_mpm.cli", "run", "--resume", "--help"],
        cwd=project_root,
        capture_output=True,
        text=True,
        env={"PYTHONPATH": str(project_root / "src")},
        check=False,
    )

    if result.returncode == 0:
        print("✅ claude-mpm run --resume --help executes without error")
    else:
        print("❌ claude-mpm run --resume --help failed")
        print(f"   Error: {result.stderr}")
        return False

    return True


def main():
    """Run the test."""
    print("=" * 60)
    print("Testing --resume Flag Actual Functionality")
    print("=" * 60)
    print()

    success = test_resume_passthrough()

    print()
    print("=" * 60)
    if success:
        print("✅ SUCCESS: --resume flag is working correctly!")
        print()
        print("Users can now use:")
        print("  claude-mpm --resume")
        print("  claude-mpm run --resume")
        print("  claude-mpm --resume -- --model opus")
    else:
        print("❌ FAILURE: --resume flag has issues")
    print("=" * 60)

    return 0 if success else 1


if __name__ == "__main__":
    sys.exit(main())
