from django.core.management.base import BaseCommand
from django.db.models import Count

from apps.herd.models import Rancher


class Command(BaseCommand):
    help = "Fast update ranchers without herds using bulk updates"

    def handle(self, *args, **kwargs):
        self.stdout.write("🔄 Bulk updating ranchers...")

        no_herd_count = Rancher.objects.annotate(
            herd_count=Count("herd")
        ).filter(herd_count=0).update(without_herd=True)

        has_herd_count = Rancher.objects.annotate(
            herd_count=Count("herd")
        ).filter(herd_count__gt=0).update(without_herd=False)

        self.stdout.write(self.style.SUCCESS(
            f"✅ Done! {no_herd_count} set to True, {has_herd_count} set to False"
        ))
