# __main__.py — CLI bridge for lib_py.agents facts and resolution (N4)

import sys, json, shlex
from lib_py.agents.registry import get_adapter, own_key, agent_of_row

def main():
    if len(sys.argv) < 2:
        print("Usage: python -m lib_py.agents <facts|resolve> [args...]", file=sys.stderr)
        sys.exit(1)

    cmd = sys.argv[1]
    if cmd == 'facts':
        agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
        adapter = get_adapter(agent_name)
        if not adapter:
            print(f"ERROR: Unknown agent {agent_name!r}", file=sys.stderr)
            sys.exit(1)
        # Emit shell-eval friendly facts
        q = shlex.quote
        print(f"MAM_AGENT_NAME={q(adapter.name)}")
        print(f"MAM_OWN_KEY={q(adapter.own_key)}")
        print(f"MAM_INPUT_PROMPT={q(adapter.input_prompt or '')}")
        print(f"MAM_INPUT_PLACEHOLDER={q(adapter.input_placeholder or '')}")
        print(f"MAM_INPUT_RULE_PATTERN={q(adapter.input_rule_pattern or '')}")
        print(f"MAM_READY_TOKENS={q(adapter.ready_tokens)}")
        print(f"MAM_STRONG_READY_TOKENS={q(adapter.strong_ready_tokens)}")
        print(f"MAM_WEAK_READY_TOKENS={q(adapter.weak_ready_tokens)}")
        print(f"MAM_MODAL_TOKENS={q(adapter.modal_tokens or '')}")
        print(f"MAM_EXIT_KEY={q(adapter.exit_key)}")
        print(f"MAM_DELEGATE_AGENT_KEY={q(adapter.delegate_agent_key)}")

    elif cmd == 'resolve':
        name = sys.argv[2] if len(sys.argv) > 2 else ''
        resolved = agent_of_row({}, session_name=name)
        if resolved:
            print(resolved)
            sys.exit(0)
        sys.exit(1)

    elif cmd == 'input-region':
        agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
        pane_text = sys.stdin.read()
        from lib_py.agents.input_region import extract_input_region
        try:
            region = extract_input_region(pane_text, agent_name)
            print(region)
            sys.exit(0)
        except Exception as ex:
            print(f"ERROR: {ex}", file=sys.stderr)
            sys.exit(5)

    elif cmd == 'spawn-spec':
        agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
        binary = sys.argv[3] if len(sys.argv) > 3 else agent_name
        uuid = sys.argv[4] if len(sys.argv) > 4 else ''
        use_wrapper = (sys.argv[5].lower() in ('1', 'true', 'yes')) if len(sys.argv) > 5 else False
        adapter = get_adapter(agent_name)
        if not adapter:
            sys.exit(1)
        print(adapter.spawn_spec(binary, uuid, use_wrapper=use_wrapper))
        sys.exit(0)

    elif cmd == 'resume-spec':
        agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
        binary = sys.argv[3] if len(sys.argv) > 3 else agent_name
        uuid = sys.argv[4] if len(sys.argv) > 4 else ''
        workspace = sys.argv[5] if len(sys.argv) > 5 else ''
        adapter = get_adapter(agent_name)
        if not adapter:
            sys.exit(1)
        from lib_py.agents.base import DiscoveryContext
        ctx = DiscoveryContext(workspace, agent_name) if workspace else None
        mat = adapter.verify_artifact(uuid, ctx) if (ctx and uuid) else False
        print(adapter.resume_spec(binary, uuid, materialized=mat))
        sys.exit(0)

    elif cmd == 'exit-key':
        agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
        adapter = get_adapter(agent_name)
        if adapter:
            print(adapter.exit_key)
            sys.exit(0)
        print('/exit')
        sys.exit(0)

    else:
        print(f"ERROR: Unknown CLI command {cmd!r}", file=sys.stderr)
        sys.exit(1)

if __name__ == '__main__':
    main()
