#!/usr/bin/env python3
"""
Fix all missing registration fee fields
"""

import os
import sys
import django
from django.db import connection

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

def fix_all_registration_fields():
    """Add all missing registration fee fields"""
    print("🔧 Checking and fixing all registration fee fields...")
    
    # All registration fee fields from the model
    required_fields = [
        ('registration_fee_amount', 'DECIMAL(10,2) NULL'),
        ('registration_fee_paid', 'BOOLEAN DEFAULT FALSE'),
        ('registration_fee_payment_date', 'DATETIME NULL'),
        ('registration_fee_payment_method', 'VARCHAR(20) NULL'),
        ('registration_fee_receipt_number', 'VARCHAR(50) NULL'),
        ('registration_fee_notes', 'TEXT NULL'),
    ]
    
    try:
        with connection.cursor() as cursor:
            for field_name, field_definition in required_fields:
                # Check if field exists
                cursor.execute("""
                    SELECT COLUMN_NAME 
                    FROM INFORMATION_SCHEMA.COLUMNS 
                    WHERE TABLE_SCHEMA = DATABASE() 
                    AND TABLE_NAME = 'users' 
                    AND COLUMN_NAME = %s
                """, [field_name])
                
                if not cursor.fetchone():
                    print(f"Adding {field_name} field...")
                    cursor.execute(f"""
                        ALTER TABLE users 
                        ADD COLUMN {field_name} {field_definition}
                    """)
                    print(f"✅ SUCCESS: Added {field_name} field")
                else:
                    print(f"✅ SUCCESS: {field_name} field already exists")
                
        print("🎉 All registration fee fields checked and fixed!")
        return True
        
    except Exception as e:
        print(f"❌ ERROR: {e}")
        return False

if __name__ == "__main__":
    success = fix_all_registration_fields()
    sys.exit(0 if success else 1)