from django.core.management.base import BaseCommand
from exams.models import Student, CreditEvent
from exams.services import points_ledger

class Command(BaseCommand):
    help = 'Allots a starting bonus of points to all active students.'

    def add_arguments(self, parser):
        parser.add_argument('--points', type=int, default=50, help='Amount of points to allot')

    def handle(self, *args, **options):
        pts = options['points']
        students = Student.objects.filter(is_active=True)
        
        count = 0
        for s in students:
            _r = points_ledger.award(
                student=s, points=pts,
                reason='Initial starting balance',
                code='STARTING_BONUS',
                evidence={},
                source='AUTO',
            )
            if _r is not None:
                count += 1
                
        self.stdout.write(self.style.SUCCESS(f"✓ Allotted {pts} points to {count} students."))
