# retoor <retoor@molodetz.nl>

from devplacepy.cli._shared import _audit_cli


def cmd_game_market_prune(args):
    from devplacepy.services.game import store

    removed = store.prune_ticks()
    _audit_cli(
        "cli.game.market.prune",
        f"CLI pruned {removed} stale Code Farm market tick(s)",
        metadata={"count": removed},
    )
    print(f"Pruned {removed} stale market tick bucket(s)")


def cmd_game_steals_prune(args):
    from devplacepy.services.game import store

    removed = store.prune_steals()
    _audit_cli(
        "cli.game.steals.prune",
        f"CLI pruned {removed} old Code Farm raid record(s)",
        metadata={"count": removed},
    )
    print(f"Pruned {removed} raid record(s)")


def cmd_game_era_status(args):
    from devplacepy.services.game import store

    era = store.active_era()
    if not era:
        print("No Era is currently running.")
        return
    print(f"Era {era['era_number']}: {era['name']}")
    print(f"Started: {era['started_at']}")
    print(f"Scheduled end: {era['ends_at']}")


def cmd_game_era_start(args):
    from devplacepy.services.game import GameError, store

    try:
        era = store.start_era(args.name, args.duration_days)
    except GameError as exc:
        print(f"Error: {exc}")
        return
    _audit_cli(
        "cli.game.era.start",
        f"CLI started Code Farm Era {era['era_number']}: {era['name']}",
        metadata={"era_number": era["era_number"], "name": era["name"]},
    )
    print(f"Started Era {era['era_number']}: {era['name']}")


def cmd_game_era_end(args):
    from devplacepy.services.game import GameError, store

    try:
        result = store.end_era()
    except GameError as exc:
        print(f"Error: {exc}")
        return
    _audit_cli(
        "cli.game.era.end",
        f"CLI ended Code Farm Era {result['era_number']}",
        metadata=result,
    )
    print(f"Ended Era {result['era_number']} ({result['participants']} participant(s) ranked)")


def register_game(subparsers):
    game = subparsers.add_parser("game", help="Code Farm management")
    game_sub = game.add_subparsers(title="action", dest="action")

    market = game_sub.add_parser("market", help="Code Farm market saturation data")
    market_sub = market.add_subparsers(title="market_action", dest="market_action")
    market_prune = market_sub.add_parser(
        "prune", help="Delete market tick buckets older than the tracking window"
    )
    market_prune.set_defaults(func=cmd_game_market_prune)

    steals = game_sub.add_parser("steals", help="Code Farm raid history")
    steals_sub = steals.add_subparsers(title="steals_action", dest="steals_action")
    steals_prune = steals_sub.add_parser(
        "prune", help="Delete raid records older than the raid-efficiency window"
    )
    steals_prune.set_defaults(func=cmd_game_steals_prune)

    era = game_sub.add_parser("era", help="Code Farm Era management")
    era_sub = era.add_subparsers(title="era_action", dest="era_action")
    era_status = era_sub.add_parser("status", help="Show the current Era status")
    era_status.set_defaults(func=cmd_game_era_status)
    era_start = era_sub.add_parser("start", help="Start a new Era")
    era_start.add_argument("name", help="Era name")
    era_start.add_argument(
        "--days", dest="duration_days", type=int, default=28, help="Planned Era length in days"
    )
    era_start.set_defaults(func=cmd_game_era_start)
    era_end = era_sub.add_parser("end", help="End the currently running Era")
    era_end.set_defaults(func=cmd_game_era_end)
