"""
Check Faith's original loan application to see original disbursement amount
"""
import pymysql

DB_CONFIG = {
    'host': 'localhost',
    'port': 3306,
    'database': 'xygbfpsg_loans',
    'user': 'xygbfpsg_graz',
    'password': 'j.ez-xy6##y.rllB',
    'charset': 'utf8mb4'
}

conn = pymysql.connect(**DB_CONFIG)

with conn.cursor() as cur:
    print("=" * 80)
    print("CHECKING ORIGINAL LOAN APPLICATION")
    print("=" * 80)
    
    # Get the loan's application
    cur.execute("""
        SELECT 
            l.loan_number,
            l.principal_amount as current_principal,
            l.interest_amount as current_interest,
            l.total_amount as current_total,
            l.processing_fee as current_proc_fee,
            l.application_id,
            l.is_rolled_over
        FROM loans l
        WHERE l.loan_number = '19112026-000001'
    """)
    
    loan = cur.fetchone()
    
    if not loan:
        print("Loan not found!")
        conn.close()
        exit()
    
    print("\nCURRENT LOAN RECORD:")
    print("  Loan Number:    {}".format(loan[0]))
    print("  Principal:      KES {:,.2f}".format(loan[1]))
    print("  Interest:       KES {:,.2f}".format(loan[2]))
    print("  Total:          KES {:,.2f}".format(loan[3]))
    print("  Proc Fee:       KES {:,.2f}".format(loan[4] if loan[4] else 0))
    print("  Application ID: {}".format(loan[5]))
    print("  Is Rolled:      {}".format(loan[6]))
    
    app_id = loan[5]
    
    if app_id:
        # Get original application details
        cur.execute("""
            SELECT 
                application_number,
                requested_amount,
                requested_duration,
                interest_amount,
                total_amount,
                processing_fee,
                submitted_at,
                status
            FROM loan_applications
            WHERE id = %s
        """, (app_id,))
        
        app = cur.fetchone()
        
        if app:
            print("\n" + "=" * 80)
            print("ORIGINAL LOAN APPLICATION:")
            print("=" * 80)
            print("  Application #:  {}".format(app[0]))
            print("  Requested Amt:  KES {:,.2f}".format(app[1]))
            print("  Duration:       {} days".format(app[2]))
            print("  Interest:       KES {:,.2f}".format(app[3]))
            print("  Total:          KES {:,.2f}".format(app[4]))
            print("  Proc Fee:       KES {:,.2f}".format(app[5] if app[5] else 0))
            print("  Submitted:      {}".format(app[6]))
            print("  Status:         {}".format(app[7]))
            
            print("\n" + "=" * 80)
            print("COMPARISON:")
            print("=" * 80)
            print("\n  Original Application:")
            print("    Principal: KES {:,.2f}".format(app[1]))
            print("    Interest:  KES {:,.2f}".format(app[3]))
            print("    Total:     KES {:,.2f}".format(app[4]))
            
            print("\n  Current Loan Record:")
            print("    Principal: KES {:,.2f}".format(loan[1]))
            print("    Interest:  KES {:,.2f}".format(loan[2]))
            print("    Total:     KES {:,.2f}".format(loan[3]))
            
            if app[1] == loan[1]:
                print("\n  ✅ Principal UNCHANGED (same as original)")
            else:
                print("\n  ⚠️  Principal CHANGED from {:,.2f} to {:,.2f}".format(app[1], loan[1]))
            
            if app[3] == loan[2]:
                print("  ✅ Interest UNCHANGED (same as original)")
            else:
                print("  ⚠️  Interest CHANGED from {:,.2f} to {:,.2f}".format(app[3], loan[2]))
            
            print("\n" + "=" * 80)
            print("FOR DISBURSEMENT REPORT:")
            print("=" * 80)
            print("\nWhat should be shown in March 2025 disbursement report?")
            print("  Option A: Original application amounts")
            print("    Principal: KES {:,.2f}".format(app[1]))
            print("  Option B: Current loan amounts")
            print("    Principal: KES {:,.2f}".format(loan[1]))
    else:
        print("\n⚠️  No application_id found for this loan")

conn.close()
