
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from hr.models import Employee

User = get_user_model()

class Command(BaseCommand):
    help = 'Fix staff status for employee users'

    def handle(self, *args, **options):
        fixed_count = 0
        
        # Get all employees with user accounts
        employees = Employee.objects.filter(user__isnull=False)
        
        for employee in employees:
            user = employee.user
            
            # Ensure employee users have is_staff=True
            if not user.is_staff:
                user.is_staff = True
                user.save()
                fixed_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'Fixed staff status for employee: {employee.full_name} ({user.username})')
                )
        
        if fixed_count == 0:
            self.stdout.write(self.style.SUCCESS('No employee users found with incorrect staff status.'))
        else:
            self.stdout.write(
                self.style.SUCCESS(f'Successfully fixed staff status for {fixed_count} employee user(s).')
            )
