from django.core.management.base import BaseCommand
from django.utils import timezone
from invoices.models import Invoice


class Command(BaseCommand):
    help = 'Update status of overdue invoices'
    
    def handle(self, *args, **options):
        self.stdout.write('Updating overdue invoices...')
        
        today = timezone.now().date()
        
        overdue_invoices = Invoice.objects.filter(
            status__in=['SENT', 'VIEWED', 'PARTIAL'],
            due_date__lt=today,
            balance_due__gt=0
        )
        
        count = overdue_invoices.update(status='OVERDUE')
        
        self.stdout.write(
            self.style.SUCCESS(f'{count} invoices updated to OVERDUE status')
        )