import os
import django
from decimal import Decimal

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

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

# Helper to print current setting and product getter
def show(product_type):
    key = f"{product_type}_processing_fee"
    val = SystemSetting.get_setting(key, None)
    print(f"SystemSetting {key} -> {val!r}")
    try:
        prod = LoanProduct.objects.filter(product_type=product_type).first()
        if not prod:
            # create a temporary product
            prod = LoanProduct.objects.create(
                name=f"Temp {product_type}",
                product_type=product_type,
                description='temp',
                min_amount=1000,
                max_amount=50000,
                interest_rate=10.0,
                processing_fee=Decimal('5.0'),
                late_payment_penalty=5.0,
                duration_months=1,
                min_duration=1,
                max_duration=30,
                available_repayment_methods=['monthly']
            )
            print('Created temp product', prod.id)
        print('LoanProduct.processing_fee field:', prod.processing_fee)
        print('LoanProduct.get_processing_fee():', prod.get_processing_fee())

        # Make a test borrower
        borrower = User.objects.filter(is_superuser=True).first()
        if not borrower:
            borrower = User.objects.create_user('testuser_for_fee', 'test@example.com', 'testpassword')

        app = LoanApplication.objects.create(
            borrower=borrower,
            loan_product=prod,
            requested_amount=10000,
            requested_duration=30,
            purpose='test',
            repayment_method='monthly'
        )
        print('Created LoanApplication id:', app.id)
        print('processing_fee_amount on application:', app.processing_fee_amount)
    except Exception as e:
        print('Error during check:', e)

if __name__ == '__main__':
    for pt in ['boost', 'boost_plus', 'mwamba', 'imara']:
        print('---', pt, '---')
        show(pt)
