from utils.models import SystemSetting
from loans.models import LoanProduct, LoanApplication
from django.contrib.auth import get_user_model
from decimal import Decimal

User = get_user_model()

# Update setting
key = 'boost_processing_fee'
SystemSetting.objects.update_or_create(key=key, defaults={'value': '8.0', 'category': 'loan'})
print('Updated SystemSetting', key, '->', SystemSetting.get_setting(key))

# Find a boost product
prod = LoanProduct.objects.filter(product_type='boost').first()
if not prod:
    prod = LoanProduct.objects.create(
        name='Temp Boost', product_type='boost', description='temp',
        min_amount=1000, max_amount=50000, interest_rate=10.0, processing_fee=Decimal('5.00'),
        late_payment_penalty=5.0, duration_months=1, min_duration=1, max_duration=30,
        available_repayment_methods=['monthly']
    )
    print('Created temp product')

print('LoanProduct.get_processing_fee():', prod.get_processing_fee())

borrower = User.objects.filter(is_superuser=True).first() or User.objects.first()
app = LoanApplication.objects.create(
    borrower=borrower,
    loan_product=prod,
    requested_amount=10000,
    requested_duration=30,
    purpose='test update',
    repayment_method='monthly'
)
print('Created application. processing_fee_amount:', app.processing_fee_amount)
print('Total amount:', app.total_amount)
