# retoor <retoor@molodetz.nl>

from devplacepy.cli._shared import _audit_cli


def cmd_quiz_prune(args):
    from datetime import datetime, timedelta, timezone

    from devplacepy.config import QUIZ_ATTEMPT_RETENTION_DAYS
    from devplacepy.services.quiz import store

    cutoff = (
        datetime.now(timezone.utc) - timedelta(days=QUIZ_ATTEMPT_RETENTION_DAYS)
    ).isoformat()
    removed = store.prune_attempts(cutoff)
    _audit_cli(
        "cli.quiz.prune",
        f"CLI pruned {removed} abandoned quiz attempt(s)",
        metadata={"count": removed, "retention_days": QUIZ_ATTEMPT_RETENTION_DAYS},
    )
    print(f"Pruned {removed} abandoned or expired quiz attempt(s)")


def register_quiz(subparsers):
    quiz = subparsers.add_parser("quiz", help="Quiz management")
    quiz_sub = quiz.add_subparsers(title="action", dest="action")
    prune = quiz_sub.add_parser(
        "prune",
        help="Delete abandoned and expired attempts older than the retention window",
    )
    prune.set_defaults(func=cmd_quiz_prune)
