from django.core.management.base import BaseCommand, CommandError
from wagtail.models import Site

from ...services import get_settings_for_site, run_mobileapp_checks


class Command(BaseCommand):
    help = "Validate mobile app readiness for a site."

    def add_arguments(self, parser):
        parser.add_argument("--site-id", type=int, required=True)

    def handle(self, *args, **options):
        site = Site.objects.filter(pk=options["site_id"]).first()
        if site is None:
            raise CommandError("Site not found.")
        checks = run_mobileapp_checks(site, get_settings_for_site(site))
        for check in checks:
            self.stdout.write(f"[{check.level.upper()}] {check.code}: {check.message}")
