import json

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

from api.services.integration.readiness import phase1_readiness


class Command(BaseCommand):
    help = "Probe Resource Booking publisher configuration and heartbeat without exposing secrets."

    def add_arguments(self, parser):
        parser.add_argument("--liveness", action="store_true")

    def handle(self, *args, **options):
        result = phase1_readiness(require_enabled=options["liveness"])
        self.stdout.write(json.dumps(result, sort_keys=True))
        if not result["ready"]:
            raise CommandError("Phase 1 publisher is not ready")
        if options["liveness"] and (
            not settings.RESOURCE_BOOKING_INTEGRATION["PUBLISH_ENABLED"]
            or not result["publisher_live"]
        ):
            raise CommandError("Phase 1 publisher is not live")
