#!/usr/bin/env python
"""
Verify that loan display shows stored database values, not recalculated values
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from loans.models import Loan
from decimal import Decimal

# Test LOAN-000193
loan_number = 'LOAN-000193'
loan = Loan.objects.get(loan_number=loan_number)

print(f"=== Testing {loan_number} ===")
print(f"\n--- Stored Database Values (CORRECT) ---")
print(f"Principal Amount: KES {loan.principal_amount:,.2f}")
print(f"Interest Amount: KES {loan.interest_amount:,.2f}")
print(f"Processing Fee: KES {loan.processing_fee:,.2f}")
print(f"Total Amount: KES {loan.total_amount:,.2f}")

print(f"\n--- Calculated Values (WRONG - uses current system rates) ---")
print(f"Interest (recalculated): KES {loan.get_display_interest_amount():,.2f}")
print(f"Processing Fee (recalculated): KES {loan.get_display_processing_fee_amount():,.2f}")
print(f"Total (recalculated): KES {loan.calculated_total_amount:,.2f}")

print(f"\n--- Expected Total ---")
expected_total = loan.principal_amount + loan.interest_amount + loan.processing_fee
print(f"Principal + Interest + Fee = {loan.principal_amount:,.2f} + {loan.interest_amount:,.2f} + {loan.processing_fee:,.2f}")
print(f"Expected Total: KES {expected_total:,.2f}")

print(f"\n--- Verification ---")
if loan.total_amount == expected_total:
    print("✓ Database total_amount is CORRECT")
else:
    print(f"✗ Database total_amount is INCORRECT (expected {expected_total:,.2f}, got {loan.total_amount:,.2f})")

if expected_total == Decimal('29344.00'):
    print("✓ Expected total matches the correct amount (KES 29,344.00)")
else:
    print(f"✗ Expected total does NOT match (expected KES 29,344.00, got KES {expected_total:,.2f})")

print("\n=== FIX APPLIED ===")
print("The template now uses:")
print("  - loan.interest_amount (stored value)")
print("  - loan.processing_fee (stored value)")
print("  - loan.total_amount (stored value)")
print("\nInstead of:")
print("  - loan.get_display_interest_amount() (recalculated)")
print("  - loan.get_display_processing_fee_amount() (recalculated)")
print("  - loan.calculated_total_amount (recalculated)")
