"""
clear.py — Remove all entries from a leaderboard table.

Usage:
    py scripts/clear.py <leaderboard>           # prompts for confirmation
    py scripts/clear.py <leaderboard> --yes     # skips confirmation (for cron/automation)

Examples:
    py scripts/clear.py daily
    py scripts/clear.py weekly --yes

Leaderboards: global, weekly, daily
"""

import argparse
import sys
import os

# Allow imports from the project root regardless of where this script is invoked from
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import create_app
from models import db, LEADERBOARD_MAP


def main():
    parser = argparse.ArgumentParser(
        description="Remove all entries from a leaderboard table.",
        epilog="Example: py scripts/clear.py daily --yes",
    )
    parser.add_argument(
        "leaderboard",
        choices=LEADERBOARD_MAP.keys(),
        help="The leaderboard to clear (global, weekly, daily).",
    )
    parser.add_argument(
        "--yes",
        action="store_true",
        help="Skip the confirmation prompt. Required for non-interactive use (e.g. cron).",
    )
    args = parser.parse_args()

    if not args.yes:
        confirm = input(f"This will delete ALL entries from '{args.leaderboard}'. Type 'yes' to continue: ")
        if confirm.strip().lower() != "yes":
            print("Aborted.")
            sys.exit(0)

    app = create_app()
    with app.app_context():
        model = LEADERBOARD_MAP[args.leaderboard]
        deleted = db.session.query(model).delete()
        db.session.commit()
        print(f"Cleared {deleted} entries from '{args.leaderboard}'.")


if __name__ == "__main__":
    main()
