"""
Verify that LOAN-000113 was fixed correctly
"""
import os
import sys

# Set production database credentials
os.environ['DB_NAME'] = 'xygbfpsg_graz'
os.environ['DB_USER'] = 'xygbfpsg_graz'
os.environ['DB_PASSWORD'] = ',qdN3O_!}oC67(]W'
os.environ['DB_HOST'] = 'localhost'
os.environ['DB_PORT'] = '3306'

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

from loans.models import Loan
from decimal import Decimal

print(f"\n{'='*80}")
print(f"VERIFYING LOAN-000113 FIX")
print(f"{'='*80}\n")

try:
    loan = Loan.objects.get(loan_number='LOAN-000113')
    
    print(f"Loan: {loan.loan_number}")
    print(f"Borrower: {loan.borrower.get_full_name()}")
    print(f"Product: {loan.application.loan_product.name}")
    print(f"Duration: {loan.duration_days} days")
    
    print(f"\nCURRENT VALUES IN DATABASE:")
    print(f"  Principal:       KES {loan.principal_amount:>12,.2f}")
    print(f"  Interest:        KES {loan.interest_amount:>12,.2f}")
    print(f"  Processing Fee:  KES {loan.processing_fee:>12,.2f}")
    print(f"  Total:           KES {loan.total_amount:>12,.2f}")
    
    # Verify calculation
    calculated_total = loan.principal_amount + loan.interest_amount + loan.processing_fee
    
    print(f"\nVERIFICATION:")
    print(f"  Calculated Total: {loan.principal_amount:,.2f} + {loan.interest_amount:,.2f} + {loan.processing_fee:,.2f}")
    print(f"                  = KES {calculated_total:,.2f}")
    
    if abs(calculated_total - loan.total_amount) < Decimal('0.01'):
        print(f"\n✅ Total is correct: {loan.total_amount:,.2f} = {calculated_total:,.2f}")
    else:
        print(f"\n❌ Total is WRONG: {loan.total_amount:,.2f} ≠ {calculated_total:,.2f}")
        print(f"   Difference: {calculated_total - loan.total_amount:,.2f}")
    
    # Check expected values
    months = loan.duration_days / 30
    expected_interest = loan.principal_amount * Decimal('0.10') * Decimal(str(months))
    expected_processing_fee = loan.principal_amount * Decimal('0.05')  # 5% from system settings
    expected_total = loan.principal_amount + expected_interest + expected_processing_fee
    
    print(f"\nEXPECTED VALUES (for {months:.2f} months):")
    print(f"  Interest (10% × {months:.2f}):  KES {expected_interest:>12,.2f}")
    print(f"  Processing Fee (5%):  KES {expected_processing_fee:>12,.2f}")
    print(f"  Total:                KES {expected_total:>12,.2f}")
    
    print(f"\nCOMPARISON:")
    if abs(loan.interest_amount - expected_interest) < Decimal('0.01'):
        print(f"  ✅ Interest is correct: {loan.interest_amount:,.2f}")
    else:
        print(f"  ❌ Interest is wrong: {loan.interest_amount:,.2f} (expected {expected_interest:,.2f})")
    
    if abs(loan.processing_fee - expected_processing_fee) < Decimal('0.01'):
        print(f"  ✅ Processing fee is correct: {loan.processing_fee:,.2f}")
    else:
        print(f"  ❌ Processing fee is wrong: {loan.processing_fee:,.2f} (expected {expected_processing_fee:,.2f})")
    
    if abs(loan.total_amount - expected_total) < Decimal('0.01'):
        print(f"  ✅ Total is correct: {loan.total_amount:,.2f}")
    else:
        print(f"  ❌ Total is wrong: {loan.total_amount:,.2f} (expected {expected_total:,.2f})")
    
    print(f"\n{'='*80}")
    if (abs(loan.interest_amount - expected_interest) < Decimal('0.01') and 
        abs(loan.processing_fee - expected_processing_fee) < Decimal('0.01') and
        abs(loan.total_amount - expected_total) < Decimal('0.01')):
        print(f"✅ LOAN-000113 IS FULLY CORRECTED!")
    else:
        print(f"❌ LOAN-000113 STILL HAS ISSUES!")
    print(f"{'='*80}\n")
    
except Loan.DoesNotExist:
    print(f"❌ Loan LOAN-000113 not found")
    sys.exit(1)
except Exception as e:
    print(f"\n❌ ERROR: {str(e)}")
    import traceback
    traceback.print_exc()
    sys.exit(1)
