# src/ctf/cli_helpers.py
# {{{ imports
import click
from ctf.helpers import detector_state
# }}}

# {{{ flag_cmd
@click.command(name="flag")
@click.option("-p", "--plain", is_flag=True, help="Print raw flag without flavor text.")
def flag_cmd(plain):
    """Retrieve the last detected flag from config."""
    # TODO add support for multiple flags and flag selection
    if detector_state["active"] is not None:
        detector_state["active"].detecting = False
    try:
        from ctf.config import load_config
        config = load_config("/home/venus/code/ctf/config.toml")
        last_flag = config.get("Competition", {}).get("last_flag", "")
        if not last_flag:
            if not plain:
                click.echo("No flag has been detected yet.")
            return
        if plain:
            click.echo(last_flag)
        else:
            click.echo(f"Last detected flag: {last_flag}")
    finally:
        if detector_state["active"] is not None:
            detector_state["active"].detecting = True
# }}}
