"""Document expiry FCM: 31/7/1 days before expiry and hourly on expiry day."""

from django.core.management.base import BaseCommand

from accounts.document_expiry_reminders import run_all_document_expiry_reminders


class Command(BaseCommand):
    help = (
        'Notify fleet owners and individual drivers at 31, 7, and 1 day(s) before expiry, '
        'and hourly on/after expiry day (idempotent per doc+expiry+window/hour/day).'
    )

    def handle(self, *args, **options):
        results = run_all_document_expiry_reminders()
        day_parts = [
            f'{days}d={count}'
            for days, count in sorted(results['days_before'].items(), reverse=True)
        ]
        day_parts.append(f'expiry_hourly={results["expiry_hourly"]}')
        total = sum(results['days_before'].values()) + results['expiry_hourly']
        detail = ', '.join(day_parts)
        self.stdout.write(self.style.SUCCESS(f'Document expiry reminders sent: {total} ({detail})'))
