"""
Add sample registration fee data to existing borrowers
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from users.models import CustomUser
from decimal import Decimal
from django.utils import timezone
import random

def add_sample_fees():
    """Add sample registration fees to borrowers"""
    print("=" * 80)
    print("ADDING SAMPLE REGISTRATION FEES")
    print("=" * 80)
    
    borrowers = CustomUser.objects.filter(role='borrower')
    
    if not borrowers.exists():
        print("\n✗ No borrowers found in the database")
        return
    
    print(f"\n✓ Found {borrowers.count()} borrowers")
    
    # Standard registration fee
    standard_fee = Decimal('500.00')
    
    # Payment methods
    payment_methods = ['mpesa', 'cash', 'bank_transfer']
    
    updated_count = 0
    
    for borrower in borrowers:
        # Randomly decide if paid or pending (80% paid, 20% pending)
        is_paid = random.random() < 0.8
        
        # Set registration fee amount
        borrower.registration_fee_amount = standard_fee
        borrower.registration_fee_paid = is_paid
        
        if is_paid:
            # Set payment details
            borrower.registration_fee_payment_method = random.choice(payment_methods)
            borrower.registration_fee_payment_date = borrower.created_at
        else:
            borrower.registration_fee_payment_method = None
            borrower.registration_fee_payment_date = None
        
        borrower.save()
        updated_count += 1
        
        status = "PAID" if is_paid else "PENDING"
        print(f"✓ Updated {borrower.get_full_name()} - Fee: KSh {standard_fee} - Status: {status}")
    
    print(f"\n" + "=" * 80)
    print(f"✓ Successfully updated {updated_count} borrowers with registration fees")
    print("=" * 80)
    
    # Show summary
    paid_count = borrowers.filter(registration_fee_paid=True).count()
    pending_count = borrowers.filter(registration_fee_paid=False).count()
    total_paid = borrowers.filter(registration_fee_paid=True).count() * standard_fee
    total_pending = borrowers.filter(registration_fee_paid=False).count() * standard_fee
    
    print(f"\nSUMMARY:")
    print(f"✓ Paid: {paid_count} borrowers - KSh {total_paid:,.2f}")
    print(f"✓ Pending: {pending_count} borrowers - KSh {total_pending:,.2f}")
    print(f"✓ Total: {borrowers.count()} borrowers - KSh {(total_paid + total_pending):,.2f}")

if __name__ == '__main__':
    add_sample_fees()
