"""
Check registration fee data in the database
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from users.models import CustomUser
from django.db.models import Count, Sum

def check_data():
    """Check registration fee data"""
    print("=" * 80)
    print("CHECKING REGISTRATION FEE DATA")
    print("=" * 80)
    
    # Check all borrowers
    all_borrowers = CustomUser.objects.filter(role='borrower')
    print(f"\n✓ Total borrowers: {all_borrowers.count()}")
    
    # Check borrowers with registration_fee_amount field
    borrowers_with_amount = all_borrowers.exclude(registration_fee_amount__isnull=True)
    print(f"✓ Borrowers with registration_fee_amount set: {borrowers_with_amount.count()}")
    
    # Check borrowers with registration_fee_amount > 0
    borrowers_with_fees = all_borrowers.filter(registration_fee_amount__gt=0)
    print(f"✓ Borrowers with registration_fee_amount > 0: {borrowers_with_fees.count()}")
    
    # Check field existence
    print("\n" + "=" * 80)
    print("CHECKING FIELD EXISTENCE")
    print("=" * 80)
    
    if all_borrowers.exists():
        sample_user = all_borrowers.first()
        print(f"\nSample user: {sample_user.get_full_name()}")
        print(f"Has registration_fee_amount field: {hasattr(sample_user, 'registration_fee_amount')}")
        print(f"Has registration_fee_paid field: {hasattr(sample_user, 'registration_fee_paid')}")
        print(f"Has registration_fee_payment_date field: {hasattr(sample_user, 'registration_fee_payment_date')}")
        print(f"Has registration_fee_payment_method field: {hasattr(sample_user, 'registration_fee_payment_method')}")
        
        if hasattr(sample_user, 'registration_fee_amount'):
            print(f"\nregistration_fee_amount value: {sample_user.registration_fee_amount}")
            print(f"registration_fee_paid value: {getattr(sample_user, 'registration_fee_paid', 'N/A')}")
    
    # Show sample of borrowers
    print("\n" + "=" * 80)
    print("SAMPLE BORROWERS (First 10)")
    print("=" * 80)
    
    for i, user in enumerate(all_borrowers[:10], 1):
        reg_fee = getattr(user, 'registration_fee_amount', None)
        reg_paid = getattr(user, 'registration_fee_paid', None)
        print(f"\n{i}. {user.get_full_name()}")
        print(f"   Phone: {user.phone_number}")
        print(f"   Registration Fee: {reg_fee}")
        print(f"   Fee Paid: {reg_paid}")
        print(f"   Created: {user.created_at}")

if __name__ == '__main__':
    check_data()
