


from django.core.management.base import BaseCommand
from apps.inventory.models import InventoryItem

class Command(BaseCommand):
    help = 'Generate asset IDs for items missing them'
    
    def handle(self, *args, **options):
        items_without_asset_id = InventoryItem.objects.filter(
            asset_id__isnull=True
        ) | InventoryItem.objects.filter(asset_id='')
        
        count = items_without_asset_id.count()
        
        if count == 0:
            self.stdout.write(
                self.style.SUCCESS('All items have asset IDs')
            )
            return
        
        self.stdout.write(f'Found {count} items without asset IDs')
        
        from apps.inventory.services import InventoryService
        service = InventoryService()
        
        updated = 0
        for item in items_without_asset_id:
            item.asset_id = service._generate_asset_id(item.business_id)
            item.save()
            updated += 1
            
            if updated % 100 == 0:
                self.stdout.write(f'  Updated {updated}/{count}...')
        
        self.stdout.write(
            self.style.SUCCESS(f'Successfully generated {updated} asset IDs')
        )