"""
Management command to embed all items in a Collection.

Usage:
    python manage.py embed_collection <collection_uid>
"""

import logging

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

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    help = "Queue embedding tasks for all items in a Collection."

    def add_arguments(self, parser):
        parser.add_argument(
            "collection_uid", type=str, help="UID of the Collection to embed"
        )

    def handle(self, *args, **options):
        collection_uid = options["collection_uid"]

        try:
            from library.models import Collection

            col = Collection.nodes.get(uid=collection_uid)
        except Exception as exc:
            raise CommandError(f"Collection not found: {collection_uid} ({exc})")

        items = col.items.all()
        self.stdout.write(f"Collection: {col.name} ({len(items)} items)")

        from library.tasks import embed_collection

        task = embed_collection.delay(collection_uid)
        self.stdout.write(
            self.style.SUCCESS(
                f"Batch task queued: {task.id} ({len(items)} items)"
            )
        )
