from django.core.management.base import BaseCommand

from apps.herd.models import Rancher


class Command(BaseCommand):
    help = "Fix Ranchers FirstName & LastName On Ranching Farm"

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

        ranchers_list = []
        ranchers = Rancher.objects.all()
        for rancher in ranchers:
            if rancher.ranching_farm:
                if not rancher.first_name or not rancher.last_name:
                    split_ranching_farm = rancher.ranching_farm.split(" ")
                    rancher.first_name = split_ranching_farm[0]
                    rancher.last_name = " ".join(split_ranching_farm[1:]) if len(split_ranching_farm) >= 2 else None
                    ranchers_list.append(rancher)
                    print(rancher.first_name, "--->", rancher.last_name)

        self.stdout.write(" Bulk updating ranchers started...")
        Rancher.objects.bulk_update(ranchers_list, fields=['first_name', 'last_name'])

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