from datetime import timedelta

from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import timezone

from apps.accounts.models import LoginAttempt


class Command(BaseCommand):
    help = "Delete stale login-attempt throttle records after the configured retention period."

    def handle(self, *args, **options):
        cutoff = timezone.now() - timedelta(days=settings.LOGIN_ATTEMPT_RETENTION_DAYS)
        deleted, _ = LoginAttempt.objects.filter(updated_at__lt=cutoff).delete()
        self.stdout.write(self.style.SUCCESS(f"Purged {deleted} stale login-attempt rows older than {settings.LOGIN_ATTEMPT_RETENTION_DAYS} days."))
