#!/usr/bin/env python
"""
Nuclear option: Remove all loans migrations and let Django recreate them
This is the most reliable approach for complex dependency issues
"""

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

def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
    django.setup()
    
    print("=== Nuclear Loans Migration Fix ===")
    print("Removing ALL loans migrations and letting Django recreate them...")
    
    with connection.cursor() as cursor:
        # Remove ALL loans migrations
        cursor.execute("DELETE FROM django_migrations WHERE app = 'loans'")
        print("✓ Removed all loans migrations")
        
        # Ensure rollover_date column exists
        try:
            cursor.execute("ALTER TABLE rollover_requests ADD COLUMN rollover_date DATE NULL COMMENT 'Preferred rollover date'")
            print("✓ Added rollover_date column")
        except Exception as e:
            if "Duplicate column name" in str(e):
                print("✓ rollover_date column already exists")
            else:
                print(f"⚠ Error adding column: {e}")
    
    print("\n=== Next Steps ===")
    print("1. Run: python manage.py makemigrations loans")
    print("2. Run: python manage.py migrate loans --fake-initial")
    print("3. Test your application")
    
    print("\n✓ Nuclear fix complete! Django will recreate all migrations in correct order.")

if __name__ == "__main__":
    main()
