#!/usr/bin/env python
"""
Script to fix migration conflicts and apply migrations on production
"""
import os
import sys
import django

# Set up Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings_production')
django.setup()

from django.core.management import call_command
from django.db import connection

def main():
    print("=" * 60)
    print("Production Migration Conflict Resolution")
    print("=" * 60)
    
    print("\nStep 1: Merging conflicting migrations...")
    print("-" * 60)
    
    try:
        # Merge all conflicting migrations
        call_command('makemigrations', '--merge', '--noinput', verbosity=2)
        print("\n✓ Migration conflicts resolved!")
        
    except Exception as e:
        print(f"\n✗ Error merging migrations: {e}")
        print("\nTrying to continue anyway...")
    
    print("\nStep 2: Applying migrations...")
    print("-" * 60)
    
    try:
        # Apply all migrations
        call_command('migrate', '--noinput', verbosity=2)
        print("\n✓ All migrations applied successfully!")
        
    except Exception as e:
        print(f"\n✗ Error applying migrations: {e}")
        print("\n" + "=" * 60)
        print("FALLBACK: Use SQL script instead")
        print("=" * 60)
        print("\nRun this command:")
        print("  python add_columns_directly.py")
        sys.exit(1)
    
    print("\n" + "=" * 60)
    print("✓ Production database is now up to date!")
    print("=" * 60)

if __name__ == '__main__':
    main()
