"""
Verify that the interest calculation fix is working correctly.
"""

import os
import django
from decimal import Decimal

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from loans.models import LoanProduct

def verify_interest_calculation():
    """Verify the calculate_interest method works correctly"""
    
    print("=" * 80)
    print("VERIFYING MWAMBA INTEREST CALCULATION FIX")
    print("=" * 80)
    
    # Get Mwamba product
    mwamba = LoanProduct.objects.get(product_type='mwamba')
    
    print(f"\nMwamba Product Settings:")
    print(f"  Interest Rate: {mwamba.interest_rate}%")
    print(f"  Processing Fee: {mwamba.processing_fee}%")
    
    # Test cases
    test_cases = [
        (Decimal('100000'), Decimal('1'), Decimal('10000'), "1 month (30 days)"),
        (Decimal('100000'), Decimal('2'), Decimal('20000'), "2 months (60 days)"),
        (Decimal('100000'), Decimal('3'), Decimal('30000'), "3 months (90 days)"),
        (Decimal('100000'), Decimal('6'), Decimal('60000'), "6 months (180 days)"),
        (Decimal('50000'), Decimal('3'), Decimal('15000'), "3 months with 50k principal"),
        (Decimal('100000'), Decimal('1.5'), Decimal('15000'), "1.5 months (45 days)"),
        (Decimal('100000'), Decimal('2.5'), Decimal('25000'), "2.5 months (75 days)"),
    ]
    
    print(f"\n" + "=" * 80)
    print("TEST RESULTS")
    print("=" * 80)
    
    all_passed = True
    
    for principal, months, expected_interest, description in test_cases:
        actual_interest = mwamba.calculate_interest(principal, months)
        
        passed = abs(actual_interest - expected_interest) < Decimal('0.01')
        status = "✓ PASS" if passed else "✗ FAIL"
        
        print(f"\n{description}:")
        print(f"  Principal: KES {principal:,.2f}")
        print(f"  Months: {months}")
        print(f"  Expected: KES {expected_interest:,.2f}")
        print(f"  Actual: KES {actual_interest:,.2f}")
        print(f"  {status}")
        
        if not passed:
            all_passed = False
            print(f"  Difference: KES {actual_interest - expected_interest:,.2f}")
    
    print(f"\n" + "=" * 80)
    print("SUMMARY")
    print("=" * 80)
    
    if all_passed:
        print("\n✓ ALL TESTS PASSED!")
        print("\nThe interest calculation is working correctly:")
        print("  Interest = Principal × 10% × Months")
        print("\nKey points:")
        print("  • 10% is charged per month")
        print("  • Months = Duration in days ÷ 30")
        print("  • This is simple interest, not compound")
        print("\nExamples:")
        print("  • 30 days (1 month): 10% × 1 = 10% total")
        print("  • 60 days (2 months): 10% × 2 = 20% total")
        print("  • 90 days (3 months): 10% × 3 = 30% total")
        print("  • 45 days (1.5 months): 10% × 1.5 = 15% total")
    else:
        print("\n✗ SOME TESTS FAILED!")
        print("The calculation logic needs to be reviewed.")
    
    return all_passed

if __name__ == '__main__':
    try:
        success = verify_interest_calculation()
        exit(0 if success else 1)
    except Exception as e:
        print(f"\n✗ Error: {str(e)}")
        import traceback
        traceback.print_exc()
        exit(1)
