from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from reports.models import LoanScoring

User = get_user_model()

class Command(BaseCommand):
    help = 'Create sample credit scores for existing clients'

    def handle(self, *args, **options):
        # Get all borrowers
        borrowers = User.objects.filter(role='borrower')
        
        created_count = 0
        for borrower in borrowers:
            # Check if credit score already exists
            if not LoanScoring.objects.filter(user=borrower).exists():
                try:
                    # Create sample credit score based on business type
                    if borrower.business_type == 'technology':
                        total_score = 85
                        risk_level = 'low'
                    elif borrower.business_type == 'transport':
                        total_score = 75
                        risk_level = 'medium'
                    elif borrower.business_type == 'agriculture':
                        total_score = 70
                        risk_level = 'medium'
                    elif borrower.business_type == 'retail':
                        total_score = 65
                        risk_level = 'medium'
                    else:
                        total_score = 60
                        risk_level = 'medium'
                    
                    credit_score = LoanScoring.objects.create(
                        user=borrower,
                        repayment_history_score=20,
                        income_score=25,
                        rollover_frequency_score=15,
                        employment_stability_score=25,
                        total_score=total_score,
                        risk_level=risk_level,
                        is_eligible=True
                    )
                    
                    # Calculate credit limit based on score
                    if total_score >= 80:
                        credit_score.credit_limit = 500000
                    elif total_score >= 60:
                        credit_score.credit_limit = 200000
                    else:
                        credit_score.credit_limit = 50000
                    
                    credit_score.save()
                    created_count += 1
                    
                    self.stdout.write(
                        self.style.SUCCESS(f'Created credit score for {borrower.get_full_name()}: {total_score}/100')
                    )
                    
                except Exception as e:
                    self.stdout.write(
                        self.style.ERROR(f'Error creating credit score for {borrower.get_full_name()}: {str(e)}')
                    )
            else:
                self.stdout.write(
                    self.style.WARNING(f'Credit score already exists for {borrower.get_full_name()}')
                )

        self.stdout.write(
            self.style.SUCCESS(f'Successfully created {created_count} credit scores')
        ) 