
from django.core.management.base import BaseCommand
from tenants.models import Tenant

class Command(BaseCommand):
    help = 'Populate tenant codes for existing tenants'

    def handle(self, *args, **options):
        tenants_without_codes = Tenant.objects.filter(tenant_code='TEMP001')
        
        for tenant in tenants_without_codes:
            tenant.tenant_code = tenant.generate_tenant_code()
            tenant.save()
            self.stdout.write(
                self.style.SUCCESS(f'Generated code {tenant.tenant_code} for tenant: {tenant.name}')
            )
        
        self.stdout.write(
            self.style.SUCCESS(f'Successfully populated codes for {tenants_without_codes.count()} tenants')
        )
