
from django.core.management.base import BaseCommand
from hr.models import Employee

class Command(BaseCommand):
    help = 'Fix missing employee_id values for existing employees'

    def handle(self, *args, **options):
        employees_without_id = Employee.objects.filter(employee_id__isnull=True) | Employee.objects.filter(employee_id='')
        
        fixed_count = 0
        for employee in employees_without_id:
            # Trigger save to auto-generate employee_id
            employee.save()
            fixed_count += 1
            self.stdout.write(
                self.style.SUCCESS(f'Fixed employee: {employee.full_name} - assigned ID: {employee.employee_id}')
            )
        
        if fixed_count == 0:
            self.stdout.write(self.style.SUCCESS('No employees found with missing IDs.'))
        else:
            self.stdout.write(
                self.style.SUCCESS(f'Successfully fixed {fixed_count} employee(s) with missing IDs.')
            )
