import sys
import pytest
from unittest.mock import patch
from ctf.main import main

def test_main_entry_point_help():
    """
    Verifies that calling main() executes the Click CLI app and responds
    to '--help' by listing registration groups.
    """
    # Mock sys.argv to simulate running 'ctf --help' from the shell
    with patch.object(sys, "argv", ["ctf", "--help"]):
        # Click calls sys.exit() after displaying help, raising SystemExit
        with pytest.raises(SystemExit) as exc_info:
            main()
        
        # Verify it exits with a successful exit code (0)
        assert exc_info.value.code == 0

def test_flag_detector_stream_detection():
    """
    Verifies that FlagDetectorStream successfully intercepts writes to output potential flags.
    """
    from io import StringIO
    from ctf.main import FlagDetectorStream
    
    out = StringIO()
    stream = FlagDetectorStream(out, r"flag\{[a-z_]+\}")
    
    stream.write("Some text before flag{my_flag_here} and text after.")
    output = out.getvalue()
    assert "Potential flag(s) detected in command output" in output
    assert "flag{my_flag_here}" in output

def test_flag_detector_stream_anchored_stripping():
    """
    Verifies that FlagDetectorStream strips standard anchors ^ and $ to support substring searches.
    """
    from io import StringIO
    from ctf.main import FlagDetectorStream
    
    out = StringIO()
    stream = FlagDetectorStream(out, r"^flag\{[a-z_]+\}$")
    
    stream.write("random flag{my_flag_here} data")
    output = out.getvalue()
    assert "Potential flag(s) detected in command output" in output
    assert "flag{my_flag_here}" in output

def test_flag_detector_stream_no_pattern():
    """
    Verifies that FlagDetectorStream passes through text unmodified if no flag pattern is configured.
    """
    from io import StringIO
    from ctf.main import FlagDetectorStream
    
    out = StringIO()
    stream = FlagDetectorStream(out, "")
    
    stream.write("normal output flag{hello}")
    assert out.getvalue() == "normal output flag{hello}"

def test_flag_detector_stream_persistence():
    """
    Verifies that FlagDetectorStream writes the detected flag to config.toml.
    """
    from io import StringIO
    from ctf.main import FlagDetectorStream
    from ctf.utils import load_config, write_config
    from pathlib import Path
    
    config_file = Path("/home/venus/code/ctf/config.toml")
    original_config = load_config(str(config_file))
    
    try:
        out = StringIO()
        stream = FlagDetectorStream(out, r"flag\{[a-z_]+\}")
        stream.write("found flag{persisted_flag} in stdout")
        
        # Reload config and check last_flag
        updated_config = load_config(str(config_file))
        assert updated_config.get("Competition", {}).get("last_flag") == "flag{persisted_flag}"
    finally:
        write_config(original_config, str(config_file))

def test_flag_cmd_cli():
    """
    Verifies the ctf flag command outputs last_flag with and without --plain.
    """
    from click.testing import CliRunner
    from ctf.main import cli
    from ctf.utils import load_config, write_config
    from pathlib import Path
    
    config_file = Path("/home/venus/code/ctf/config.toml")
    original_config = load_config(str(config_file))
    
    try:
        # Pre-set last_flag in config
        temp_config = load_config(str(config_file))
        if "Competition" not in temp_config:
            temp_config["Competition"] = {}
        temp_config["Competition"]["last_flag"] = "flag{test_cli_flag}"
        write_config(temp_config, str(config_file))
        
        runner = CliRunner()
        
        # Test standard flag command output
        result_std = runner.invoke(cli, ["flag"])
        assert result_std.exit_code == 0
        assert "Last detected flag: flag{test_cli_flag}" in result_std.output
        
        # Test plain flag command output (no flavor text, has newline)
        result_plain = runner.invoke(cli, ["flag", "--plain"])
        assert result_plain.exit_code == 0
        assert result_plain.output.strip() == "flag{test_cli_flag}"
    finally:
        write_config(original_config, str(config_file))


def test_flag_cmd_exemption(capsys):
    """
    Verifies that running ctf flag is exempted from triggering the stdout flag warning box.
    """
    from ctf.main import main
    from ctf.utils import load_config, write_config
    from pathlib import Path
    import sys
    from unittest.mock import patch
    import pytest
    
    config_file = Path("/home/venus/code/ctf/config.toml")
    original_config = load_config(str(config_file))
    
    try:
        temp_config = load_config(str(config_file))
        if "Competition" not in temp_config:
            temp_config["Competition"] = {}
        temp_config["Competition"]["flag_format"] = r"flag\{[a-z_]+\}"
        temp_config["Competition"]["last_flag"] = "flag{test_exempt_flag}"
        write_config(temp_config, str(config_file))
        
        with patch.object(sys, "argv", ["ctf", "flag"]):
            with pytest.raises(SystemExit) as exc_info:
                main()
            assert exc_info.value.code == 0
            
        captured = capsys.readouterr()
        # Verify that the warning box is NOT in stdout
        assert "Potential flag(s) detected in command output" not in captured.out
        # Verify that the actual flag info IS printed
        assert "Last detected flag: flag{test_exempt_flag}" in captured.out
    finally:
        write_config(original_config, str(config_file))



