#!/usr/bin/env python3
"""
Loan Settings Verification Script
================================

This script verifies that loan settings are correctly configured.

Usage:
    python verify_loan_settings.py
"""

import os
import django
from decimal import Decimal

# Set up Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from loans.models import LoanProduct
from utils.models import SystemSetting

def verify_product_settings():
    """Verify loan product settings"""
    print("🔍 Verifying Loan Product Settings")
    print("=" * 50)
    
    products = LoanProduct.objects.all()
    
    for product in products:
        print(f"\n📋 {product.name} ({product.product_type})")
        print("-" * 30)
        
        # Get settings values
        interest_rate = product.get_interest_rate()
        processing_fee = product.get_processing_fee()
        
        print(f"Interest Rate: {interest_rate}%")
        print(f"Processing Fee: {processing_fee}%")
        
        # Test calculations
        test_amount = Decimal('5000')
        interest = product.calculate_interest(test_amount, 1)
        processing = product.calculate_processing_fee(test_amount, 1)
        
        print(f"Test Calculation (KES {test_amount}):")
        print(f"  Interest (1 month): KES {interest}")
        print(f"  Processing Fee: KES {processing}")
        print(f"  Total: KES {test_amount + interest + processing}")
        
        # Check if values are correct
        expected_interest = test_amount * Decimal('0.20')  # 20%
        expected_processing = test_amount * Decimal('0.02')  # 2%
        
        if product.product_type == 'imara':
            expected_interest = test_amount * Decimal('0.10')  # 10% for Imara
        
        interest_correct = interest == expected_interest
        processing_correct = processing == expected_processing
        
        print(f"  Interest Correct: {'✅' if interest_correct else '❌'}")
        print(f"  Processing Correct: {'✅' if processing_correct else '❌'}")

def verify_system_settings():
    """Verify system settings"""
    print("\n🔧 Verifying System Settings")
    print("=" * 50)
    
    settings_to_check = [
        'boost_interest_rate',
        'boost_processing_fee',
        'boost_plus_interest_rate',
        'boost_plus_processing_fee',
        'mwamba_interest_rate',
        'mwamba_processing_fee',
        'imara_interest_rate',
        'imara_processing_fee'
    ]
    
    for key in settings_to_check:
        try:
            value = SystemSetting.get_float(key, 0)
            print(f"{key}: {value}%")
        except Exception as e:
            print(f"{key}: ERROR - {str(e)}")

def main():
    """Main function"""
    print("🔍 Loan Settings Verification")
    print("=" * 50)
    
    verify_system_settings()
    verify_product_settings()
    
    print("\n✅ Verification completed!")

if __name__ == "__main__":
    main()
