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 registration fee for boost
key = 'boost_registration_fee'
SystemSetting.objects.update_or_create(key=key, defaults={'value': '2.5', 'category': 'loan'})
print('Updated SystemSetting', key, '->', SystemSetting.get_setting(key))

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_registration_fee():', prod.get_registration_fee())

borrower = User.objects.filter(is_superuser=True).first() or User.objects.first()
app = LoanApplication.objects.create(
    borrower=borrower,
    loan_product=prod,
    requested_amount=20000,
    requested_duration=30,
    purpose='test registration fee',
    repayment_method='monthly'
)
print('Created application. registration_fee_amount:', app.registration_fee_amount)
print('processing_fee_amount:', app.processing_fee_amount)
print('total_amount:', app.total_amount)
