# ucc_py/__main__.py

import argparse
import json
import sys
from pathlib import Path

from .core.differ import BaselineManager, Differ

def main():
    """
    Main entry point for the ucc-py CLI application.
    """
    raw = sys.argv[1:]
    # If the user invoked the dedicated 'differ' subcommand, build a parser for it.
    if raw and raw[0] == "differ":
        parser = argparse.ArgumentParser(description="differ utilities")
        subparsers = parser.add_subparsers(dest="differ_cmd")

        create_sp = subparsers.add_parser("create", help="Create a baseline from a directory or git commit")
        create_sp.add_argument("path", type=Path, help="Path to project directory or git repo")
        create_sp.add_argument("--git-ref", dest="git_ref", default=None, help="If set, export given git ref as baseline")
        create_sp.add_argument("--snapshot", action="store_true", default=True, help="Store snapshot of files (default: True)")
        create_sp.add_argument("--ignore", dest="ignore", nargs='*', default=None, help="Ignore patterns to exclude from baseline (e.g. *.pyc)")

        diff_sp = subparsers.add_parser("diff", help="Run diff against an existing baseline")
        diff_sp.add_argument("baseline_id", help="Baseline id to compare against")
        diff_sp.add_argument("path", type=Path, help="Current project path to compare")

        args = parser.parse_args()
        if args.differ_cmd == "create":
            path = str(args.path)
            bm = BaselineManager(path)
            ignore_patterns = args.ignore if getattr(args, 'ignore', None) else None
            if args.git_ref:
                baseline_id = bm.create_baseline_from_git(path, commit_ref=args.git_ref, baseline_id=None, snapshot=args.snapshot, ignore_patterns=ignore_patterns)
            else:
                baseline_id = bm.create_baseline_from_dir(path, baseline_id=None, snapshot=args.snapshot, ignore_patterns=ignore_patterns)
            print(f"Baseline created: {baseline_id}")
            return
        elif args.differ_cmd == "diff":
            baseline_id = args.baseline_id
            path = str(args.path)
            bm = BaselineManager(path)
            meta = bm.load_metadata(baseline_id)
            d = Differ(meta, path)
            result = d.diff()
            print(json.dumps(result, indent=2))
            return
        else:
            parser.print_help()
            return

    # Legacy CLI mode (keep backward-compatible behavior)
    parser = argparse.ArgumentParser(
        description="A Python tool to replicate and enhance UCC functionalities."
    )
    parser.add_argument("--gui", action="store_true", help="Launch the graphical user interface (Tkinter).")
    parser.add_argument("baseline_dirs", metavar="DIR", nargs="*", type=Path, help="One (for analysis) or two (for diff) directories to process.")
    parser.add_argument("--outdir", type=Path, default=Path("./ucc_py_output"), help="Directory to store output reports.")

    args = parser.parse_args()

    # If user asked for GUI, or no positional directories were provided, launch GUI and exit
    if args.gui or len(args.baseline_dirs or []) == 0:
        try:
            from .gui.gui import run_app
        except Exception as e:
            print(f"Errore avviando la GUI: {e}", file=sys.stderr)
            sys.exit(1)
        run_app()
        return

    # --- Argument Validation for legacy CLI mode ---
    if len(args.baseline_dirs) > 2:
        print("Error: Maximum of two baseline directories are supported.", file=sys.stderr)
        sys.exit(1)
    for directory in args.baseline_dirs:
        if not directory.is_dir():
            print(f"Error: Path '{directory}' is not a valid directory.", file=sys.stderr)
            sys.exit(1)

    print(f"Starting analysis for: {[str(d) for d in args.baseline_dirs]}")
    print(f"Output will be saved to: {args.outdir}")
    args.outdir.mkdir(parents=True, exist_ok=True)

if __name__ == "__main__":
    main()