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

from base.contact_submission_processor import process_contact_submissions


class Command(BaseCommand):
    help = "Scan and send queued contact form submissions"

    def add_arguments(self, parser):
        parser.add_argument("--limit", type=int, default=settings.CONTACT_FORM_WORKER_BATCH_SIZE)
        parser.add_argument(
            "--max-runtime-seconds",
            type=int,
            default=settings.CONTACT_FORM_WORKER_MAX_RUNTIME_SECONDS,
        )

    def handle(self, *args, **options):
        result = process_contact_submissions(
            limit=options["limit"],
            max_runtime_seconds=options["max_runtime_seconds"],
        )
        cooldown_until = result["cooldown_until"].isoformat() if result["cooldown_until"] else ""
        self.stdout.write(
            "processed={processed} allowed={allowed} blocked={blocked} sent={sent} "
            "failed={failed} disabled={disabled} detector_busy={detector_busy} "
            "cooldown_until={cooldown}".format(
                **result,
                cooldown=cooldown_until,
            )
        )
