"""CLI entrypoint: `python -m trammel` or `trammel` console script."""

from __future__ import annotations

import argparse
import json
import os
import sys

from . import __version__, explore, plan_and_execute
from .utils import DEFAULT_DB_PATH


def main() -> None:
    parser = argparse.ArgumentParser(
        description="Trammel: dependency-aware planning with bounded beam exploration.",
    )
    parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
    parser.add_argument("goal", nargs="?", default=None, help="High-level goal string")
    parser.add_argument("--root", default=os.getcwd(), help="Project root (default: cwd)")
    parser.add_argument("--beams", type=int, default=3, help="Requested beam count (capped by CPU)")
    parser.add_argument("--db", default=DEFAULT_DB_PATH, help="SQLite path for recipes and plans")
    parser.add_argument(
        "--test-cmd", nargs="+", default=None,
        help="Custom test command (default: unittest discover)",
    )
    parser.add_argument("--language", default=None, help="Project language (auto-detected if omitted)")
    parser.add_argument("--scope", default=None, help="Subdirectory scope for analysis (monorepo support)")
    parser.add_argument("--dry-run", action="store_true", help="Preview decomposition without running tests")
    args = parser.parse_args()

    if not os.path.isdir(args.root):
        print(f"Error: project root does not exist: {args.root}", file=sys.stderr)
        sys.exit(1)

    if args.goal is None:
        if sys.stdin.isatty():
            parser.print_help()
            sys.exit(2)
        try:
            payload = json.load(sys.stdin)
        except json.JSONDecodeError as exc:
            print(f"Error: invalid JSON on stdin: {exc}", file=sys.stderr)
            sys.exit(1)
        if not isinstance(payload, dict):
            print("Error: JSON payload must be an object with a 'goal' key", file=sys.stderr)
            sys.exit(1)
        goal_raw = payload.get("goal")
        if not goal_raw or not isinstance(goal_raw, str):
            print("Error: JSON payload must contain a non-empty string 'goal' key", file=sys.stderr)
            sys.exit(1)
        goal = goal_raw
    else:
        goal = args.goal

    if args.dry_run:
        result = explore(
            goal, args.root, num_beams=args.beams, db_path=args.db,
            language=args.language, scope=args.scope,
        )
    else:
        result = plan_and_execute(
            goal, args.root, num_beams=args.beams, db_path=args.db,
            test_cmd=args.test_cmd, language=args.language, scope=args.scope,
        )
    print(json.dumps(result, indent=2))
