#!/usr/bin/env python3
"""
Uniform Loan Display Fix
========================

This script ensures all loans display current system settings for interest rates
and processing fees, regardless of when they were created.

The goal is uniformity - all loans should show the same rates as defined in settings.
"""

import os
import django

# Set up Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from loans.models import Loan
from utils.models import SystemSetting

def update_loan_display_methods():
    """Add methods to Loan model to always return current system settings"""
    
    # This would be added to the Loan model
    loan_model_code = '''
    def get_display_interest_rate(self):
        """Get current interest rate from system settings for display"""
        return self.application.loan_product.get_interest_rate()
    
    def get_display_processing_fee_rate(self):
        """Get current processing fee rate from system settings for display"""
        return self.application.loan_product.get_processing_fee()
    
    def get_display_processing_fee_amount(self):
        """Calculate processing fee using current system rate for display"""
        current_rate = self.get_display_processing_fee_rate()
        return self.principal_amount * (current_rate / 100)
    
    def get_display_interest_amount(self):
        """Calculate interest using current system rate for display"""
        current_rate = self.get_display_interest_rate()
        months = max(1, self.duration_days / 30)
        return self.principal_amount * (current_rate / 100) * months
    '''
    
    print("📝 Add these methods to your Loan model in loans/models.py:")
    print("=" * 60)
    print(loan_model_code)
    print("=" * 60)

def update_template_code():
    """Show template updates needed"""
    
    template_code = '''
    <!-- In templates/loans/loan_detail.html, replace: -->
    
    <!-- OLD (shows stored values): -->
    <dt class="text-sm font-medium text-gray-500">Interest Rate</dt>
    <dd class="mt-1 text-sm text-gray-900">{{ loan.application.loan_product.get_interest_rate }}%</dd>
    
    <dt class="text-sm font-medium text-gray-500">Processing Fee</dt>
    <dd class="mt-1 text-sm text-gray-900">KES {{ loan.processing_fee|floatformat:2 }}</dd>
    
    <!-- NEW (shows current system rates): -->
    <dt class="text-sm font-medium text-gray-500">Interest Rate</dt>
    <dd class="mt-1 text-sm text-gray-900">{{ loan.get_display_interest_rate }}%</dd>
    
    <dt class="text-sm font-medium text-gray-500">Processing Fee</dt>
    <dd class="mt-1 text-sm text-gray-900">KES {{ loan.get_display_processing_fee_amount|floatformat:2 }} ({{ loan.get_display_processing_fee_rate }}%)</dd>
    '''
    
    print("📝 Update your template in templates/loans/loan_detail.html:")
    print("=" * 60)
    print(template_code)
    print("=" * 60)

def show_current_settings():
    """Show current system settings"""
    print("🔍 Current System Settings:")
    print("=" * 40)
    
    products = ['boost', 'boost_plus', 'mwamba', 'imara']
    
    for product in products:
        interest_key = f"{product}_interest_rate"
        processing_key = f"{product}_processing_fee"
        
        interest_rate = SystemSetting.get_float(interest_key, 0)
        processing_rate = SystemSetting.get_float(processing_key, 0)
        
        print(f"{product.upper()}:")
        print(f"  Interest Rate: {interest_rate}%")
        print(f"  Processing Fee: {processing_rate}%")
        print()

def main():
    """Main function"""
    print("🎯 Uniform Loan Display Fix")
    print("=" * 50)
    print("This will make all loans display current system settings")
    print("for interest rates and processing fees, regardless of when created.")
    print()
    
    # Show current settings
    show_current_settings()
    
    # Show model updates needed
    update_loan_display_methods()
    
    # Show template updates needed
    update_template_code()
    
    print("✅ After implementing these changes:")
    print("  - All loans will show current system interest rates")
    print("  - All loans will show current system processing fee rates")
    print("  - Stored amounts remain unchanged (for accounting)")
    print("  - Display will be uniform across all loans")

if __name__ == "__main__":
    main()
