"""Daily finance automation: due reminders and credit follow-ups."""
from django.core.management.base import BaseCommand

from finance_and_accounting.finance_extended import generate_due_reminders
from system_administration.models import CompanyBranch


class Command(BaseCommand):
    help = "Generate AR/AP due reminders and credit follow-up queue for all active branches."

    def handle(self, *args, **options):
        total = 0
        for branch in CompanyBranch.objects.filter(recycle_bin=False, branch_active=True):
            result = generate_due_reminders(branch)
            count = result.get("reminders_created", 0)
            total += count
            if count:
                self.stdout.write(f"Branch {branch.branch_name}: {count} reminders")
        self.stdout.write(self.style.SUCCESS(f"Done — {total} reminders created."))
