"""
Get Faith's original loan application 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("FAITH'S ORIGINAL LOAN APPLICATION")
    print("=" * 80)
    
    # First discover the table structure
    print("\n[INFO] Checking loan_applications table structure...")
    cur.execute("DESCRIBE loan_applications")
    columns = [col[0] for col in cur.fetchall()]
    print("[OK] Columns: {}".format(', '.join(columns)))
    
    # Get Faith's loan application using SELECT *
    print("\n[INFO] Getting Faith's application...")
    cur.execute("""
        SELECT *
        FROM loan_applications
        WHERE id = '2b47cc9dd9254875b18a9ff0d10149c0'
    """)
    
    app = cur.fetchone()
    col_names = [desc[0] for desc in cur.description]
    
    if app:
        print("\n[OK] Application found!")
        print("\nAPPLICATION DETAILS:")
        print("-" * 80)
        
        # Print all columns and values
        for col_name, value in zip(col_names, app):
            if col_name in ['requested_amount', 'interest_amount', 'total_amount']:
                print("  {}: KES {:,.2f}".format(col_name.ljust(25), value if value else 0))
            elif col_name in ['id', 'borrower_id', 'loan_id']:
                print("  {}: {}".format(col_name.ljust(25), value))
            elif 'date' in col_name.lower() or 'at' in col_name.lower():
                print("  {}: {}".format(col_name.ljust(25), value))
            elif col_name in ['application_number', 'status']:
                print("  {}: {}".format(col_name.ljust(25), value))
        
        # Find the requested_amount specifically
        if 'requested_amount' in col_names:
            idx = col_names.index('requested_amount')
            original_amount = app[idx]
            
            print("\n" + "=" * 80)
            print("ORIGINAL DISBURSEMENT AMOUNT")
            print("=" * 80)
            print("\n✅ Original Amount Requested: KES {:,.2f}".format(original_amount if original_amount else 0))
            
            # Compare with current loan
            cur.execute("""
                SELECT principal_amount
                FROM loans
                WHERE loan_number = '19112026-000001'
            """)
            current_principal = cur.fetchone()[0]
            
            print("   Current Principal:         KES {:,.2f}".format(current_principal))
            print("\n   Difference: KES {:,.2f}".format((original_amount if original_amount else 0) - current_principal))
            
            if original_amount and original_amount > current_principal:
                print("\n✅ CONFIRMED: Original disbursement was higher than current principal")
                print("   This confirms payments were made before rollover")
                print("\n   For DISBURSEMENT REPORT, should show: KES {:,.2f}".format(original_amount))
                print("   Currently probably showing:           KES {:,.2f}".format(current_principal))
    else:
        print("\n❌ Application not found")

conn.close()
