#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""LAUNCH the mapping stage — one command, end to end.

    python run_mapping.py --client drumer --window-title "Data Car"
    python run_mapping.py --client drumer --exe "C:\\Program Files\\DataCar\\Dcwin.exe"
    python run_mapping.py --client drumer --window-title "Data Car" --provider local

What happens: makes the process DPI-aware, attaches to (or launches) the target
window, then runs the autonomous Mapper — a BRAIN model (via the Gateway) drives
the tools, explores the app by itself, and writes:

    clients/<client>/software-map.json   — the evidence-grounded map
    clients/<client>/REVIEW.md           — the first human-readable review
    clients/<client>/session/*.png       — every screenshot taken
    clients/<client>/mapping_run.log     — the action log

No human in the loop, no Claude Code. Safety (no destructive clicks) is enforced
inside the tool layer. Switch OpenRouter<->local in model_config.json (or --provider).
"""

from __future__ import annotations

import argparse
import sys

from mapper_agent import MapperAgent


def main(argv=None):
    ap = argparse.ArgumentParser(description="RPA Mapper — autonomous app mapping")
    ap.add_argument("--client", required=True, help="client id (folder under clients/)")
    g = ap.add_mutually_exclusive_group(required=True)
    g.add_argument("--exe", help="path to the target .exe to launch")
    g.add_argument("--window-title", help="regex of an OPEN window title to attach to")
    g.add_argument("--target", help="human name of the app; it is found+opened automatically")
    ap.add_argument("--username", default=None, help="login username (if the app needs login)")
    ap.add_argument("--password", default=None, help="login password (if the app needs login)")
    ap.add_argument("--provider", choices=["openrouter", "local"], default=None,
                    help="override active_provider from model_config.json")
    ap.add_argument("--max-steps", type=int, default=140)
    ap.add_argument("--max-minutes", type=int, default=40)
    ap.add_argument("--strict-modal", action="store_true",
                    help="also block clicks on unreadable targets inside dialogs")
    args = ap.parse_args(argv)

    config = {"gated_mode": True, "block_unknown_in_modal": bool(args.strict_modal)}
    login = {"username": args.username, "password": args.password} \
        if args.username else None
    agent = MapperAgent(
        client=args.client,
        exe_path=args.exe,
        window_title=args.window_title,
        target_software=args.target,
        login=login,
        provider=args.provider,
        max_steps=args.max_steps,
        max_minutes=args.max_minutes,
        config=config,
    )
    print("=" * 70)
    print("RPA Mapper — client=%s  provider=%s" %
          (args.client, args.provider or "(config default)"))
    print("gateway:", agent.gw.describe())
    print("=" * 70)
    ok = agent.run()
    print("=" * 70)
    print("DONE." if ok else "ENDED WITH ERRORS.")
    print("Review:  clients/%s/REVIEW.md" % args.client)
    print("Map:     clients/%s/software-map.json" % args.client)
    return 0 if ok else 1


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