"""MusicIndexer CLI. Two main workflows plus granular helpers.

    python main.py fetch       # do all the work: pull + scan + parse + enrich + match
    python main.py display     # open the GUI to see what you're missing

`fetch` is incremental and DB-backed (see Env.py): the first run is slow
(initial scan + MusicBrainz lookups), later runs only touch what changed.
Helpers: `enrich` (resume MBID resolution), `match` (print the summary).
"""
import argparse
import logging
import sys


def _setup_logging():
    logging.basicConfig(level=logging.INFO, format="%(message)s")


def cmd_fetch(args):
    _setup_logging()
    from src.workflows import run_fetch

    run_fetch(enrich_limit=args.enrich_limit, enrich=not args.no_enrich,
              spotify_full=args.spotify_full, include_top=args.include_top)


def cmd_display(args):
    from src.workflows import run_display

    run_display()


def cmd_enrich(args):
    _setup_logging()
    from src.db.database import init_db
    from src.enrich.mbid import resolve_mbids, unresolved_count

    init_db()
    print(f"unresolved before: {unresolved_count(args.source)}")
    print(f"resolved this run: {resolve_mbids(limit=args.limit, source=args.source)}")
    print(f"unresolved after:  {unresolved_count(args.source)}")


def cmd_match(args):
    import json

    from src.match.matcher import summary

    print(json.dumps(summary(), indent=2))


def build_parser():
    parser = argparse.ArgumentParser(prog="musicindexer", description=__doc__)
    sub = parser.add_subparsers(dest="cmd", required=True)

    fetch = sub.add_parser("fetch", help="incrementally fetch + scan + enrich + match")
    fetch.add_argument("--enrich-limit", type=int, default=None,
                       help="cap MBID lookups this run (default: resolve all)")
    fetch.add_argument("--no-enrich", action="store_true",
                       help="skip MusicBrainz enrichment; use MBIDs already on tracks")
    fetch.add_argument("--spotify-full", action="store_true",
                       help="force a full Spotify resync (ignore snapshot/watermark)")
    fetch.add_argument("--include-top", action="store_true",
                       help="also pull the top-tracks endpoint (off by default; "
                            "with --spotify-full, top-only tracks are otherwise pruned)")

    sub.add_parser("display", help="open the Streamlit GUI")

    enrich = sub.add_parser("enrich", help="resume MBID resolution only")
    enrich.add_argument("--limit", type=int, default=None)
    enrich.add_argument("--source", choices=["spotify", "itunes", "local"], default=None)

    sub.add_parser("match", help="print present/missing summary")
    return parser


def main():
    args = build_parser().parse_args()
    try:
        {
            "fetch": cmd_fetch,
            "display": cmd_display,
            "enrich": cmd_enrich,
            "match": cmd_match,
        }[args.cmd](args)
    except KeyboardInterrupt:
        # Ctrl+C: stop cleanly, no traceback. Work already committed is kept
        # (each step writes incrementally), so just re-run to continue.
        print("\ninterrupted — partial progress saved; re-run to continue.",
              file=sys.stderr)
        sys.exit(130)


if __name__ == "__main__":
    main()
