from django.core.management.base import BaseCommand
from payments.models import Invoice
from utils.notifications import send_sms
from datetime import date


class Command(BaseCommand):
    help = 'Send rent reminders'

    def handle(self, *args, **kwargs):
        today = date.today()

        invoices = Invoice.objects.filter(status__in=['pending', 'overdue'])

        for invoice in invoices:
            days_left = (invoice.due_date - today).days

            if days_left <= 3:
                send_sms(
                    invoice.tenant.phone_number,
                    f"Reminder: Rent due in {days_left} days"
                )

        self.stdout.write("Reminders sent")