"""
Verify that the utils_auditlog table fix is complete
"""
import os
import django

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

from django.db import connection

def verify_fix():
    """Verify the fix is complete"""
    print("=" * 60)
    print("VERIFICATION: utils_auditlog Table Fix")
    print("=" * 60)
    
    with connection.cursor() as cursor:
        # Check if table exists
        cursor.execute("""
            SELECT COUNT(*)
            FROM information_schema.tables 
            WHERE table_schema = DATABASE()
            AND table_name = 'utils_auditlog'
        """)
        result = cursor.fetchone()
        
        if result[0] > 0:
            print("\n✓ Table 'utils_auditlog' exists")
            
            # Check table structure
            cursor.execute("DESCRIBE utils_auditlog")
            columns = cursor.fetchall()
            
            print("\n✓ Table structure:")
            for col in columns:
                print(f"    {col[0]:<20} {col[1]:<20}")
            
            # Check foreign key
            cursor.execute("""
                SELECT 
                    CONSTRAINT_NAME,
                    REFERENCED_TABLE_NAME,
                    REFERENCED_COLUMN_NAME
                FROM information_schema.KEY_COLUMN_USAGE
                WHERE TABLE_SCHEMA = DATABASE()
                AND TABLE_NAME = 'utils_auditlog'
                AND REFERENCED_TABLE_NAME IS NOT NULL
            """)
            fks = cursor.fetchall()
            
            if fks:
                print("\n✓ Foreign key constraints:")
                for fk in fks:
                    print(f"    {fk[0]} -> {fk[1]}.{fk[2]}")
            
            # Check indexes
            cursor.execute("""
                SELECT DISTINCT INDEX_NAME
                FROM information_schema.STATISTICS
                WHERE TABLE_SCHEMA = DATABASE()
                AND TABLE_NAME = 'utils_auditlog'
                AND INDEX_NAME != 'PRIMARY'
            """)
            indexes = cursor.fetchall()
            
            if indexes:
                print("\n✓ Indexes:")
                for idx in indexes:
                    print(f"    {idx[0]}")
            
            print("\n" + "=" * 60)
            print("✓ ALL CHECKS PASSED!")
            print("=" * 60)
            print("\nYou can now try logging in. The error should be resolved.")
            print("\nTo test, run your Django development server:")
            print("  python manage.py runserver")
            print("\nThen navigate to: http://127.0.0.1:8000/login/")
            
            return True
        else:
            print("\n✗ ERROR: Table 'utils_auditlog' does not exist!")
            print("Please run: python create_auditlog_final.py")
            return False

if __name__ == '__main__':
    verify_fix()
