"""
Management command to clean up old notifications.

Deletes read/dismissed notifications older than the global max age,
and removes expired notifications. Can be scheduled via cron or
Kubernetes CronJob.

Usage:
    python manage.py cleanup_notifications
    python manage.py cleanup_notifications --max-age-days=60
"""
from django.core.management.base import BaseCommand

from themis.notifications import cleanup_notifications


class Command(BaseCommand):
    help = "Delete old read/dismissed/expired notifications"

    def add_arguments(self, parser):
        parser.add_argument(
            "--max-age-days",
            type=int,
            default=None,
            help=(
                "Override the maximum age in days for read/dismissed "
                "notifications. Defaults to THEMIS_NOTIFICATION_MAX_AGE_DAYS "
                "setting (90 days)."
            ),
        )

    def handle(self, *args, **options):
        max_age_days = options["max_age_days"]
        deleted = cleanup_notifications(max_age_days=max_age_days)
        self.stdout.write(
            self.style.SUCCESS(f"Cleaned up {deleted} notification(s).")
        )
