import os
import django
from decimal import Decimal

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
try:
    django.setup()
except Exception as e:
    print('Django setup error:', e)
    raise

from utils.models import SystemSetting
from loans.models import LoanProduct, LoanApplication
from django.contrib.auth import get_user_model
User = get_user_model()

for pt in ['boost', 'boost_plus', 'mwamba', 'imara']:
    key = f"{pt}_processing_fee"
    val = SystemSetting.get_setting(key, None)
    print(f"SystemSetting {key} -> {val!r}")
    prod = LoanProduct.objects.filter(product_type=pt).first()
    if prod:
        print('LoanProduct.processing_fee field:', prod.processing_fee)
        print('LoanProduct.get_processing_fee():', prod.get_processing_fee())
    else:
        print('No LoanProduct found for', pt)

# Create a test application using first superuser or first user
borrower = User.objects.filter(is_superuser=True).first() or User.objects.first()
prod = LoanProduct.objects.filter(product_type='boost').first()
if prod and borrower:
    app = LoanApplication.objects.create(
        borrower=borrower,
        loan_product=prod,
        requested_amount=10000,
        requested_duration=30,
        purpose='test',
        repayment_method='monthly'
    )
    print('\nCreated LoanApplication:')
    print('processing_fee_amount:', app.processing_fee_amount)
    print('registration_fee_amount:', app.registration_fee_amount)
    print('total_amount:', app.total_amount)
else:
    print('\nCould not create LoanApplication for testing (missing product or user).')
