from django.core.management.base import BaseCommand

from store.db import get_db
from store.indexnow import ping


class Command(BaseCommand):
    help = "Submit every product, category and landing page URL to IndexNow (run once after launch)."

    def handle(self, *args, **opts):
        from store import seo_hubs
        db = get_db()
        paths = ["/", "/shop", "/laptop-prices-kenya", "/models", "/guides", "/faq"]
        paths += [f"/category/{c['slug']}" for c in db.categories.find({}, {"slug": 1})]
        paths += [f"/product/{p['slug']}" for p in db.products.find({"active": True}, {"slug": 1})]
        paths += [f"/models/{s}" for s in seo_hubs.model_groups()]
        paths += [f"/shop/{c['slug']}" for c in seo_hubs.collection_index()]
        ping(paths)
        self.stdout.write(self.style.SUCCESS(f"Submitted {len(paths)} URLs to IndexNow"))
