#!/usr/bin/env python
"""
Simple production deployment script for rollover enhancements
One-liner command to fix migration issues and deploy rollover functionality
"""

import os
import sys
import django
from django.core.management import execute_from_command_line
from django.db import connection

def main():
    """Simple deployment function"""
    print("=== Quick Rollover Enhancement Deployment ===")
    
    # Setup Django
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
    django.setup()
    
    try:
        with connection.cursor() as cursor:
            # Fix migration history
            print("Fixing migration history...")
            cursor.execute("DELETE FROM django_migrations WHERE app = 'users' AND name = '0011_manual_add_is_default'")
            
            # Add rollover_date column if it doesn't exist
            print("Adding rollover_date column...")
            cursor.execute("""
                ALTER TABLE rollover_requests 
                ADD COLUMN rollover_date DATE NULL 
                COMMENT 'Preferred rollover date'
            """)
            
            # Create migration record
            print("Creating migration record...")
            cursor.execute("""
                INSERT IGNORE INTO django_migrations (app, name, applied) 
                VALUES ('loans', '0018_add_rollover_date_field', NOW())
            """)
            
        # Run migrations
        print("Running migrations...")
        execute_from_command_line(['manage.py', 'migrate', '--noinput'])
        
        print("SUCCESS: Rollover enhancements deployed!")
        return True
        
    except Exception as e:
        if "Duplicate column name" in str(e):
            print("Column already exists, continuing...")
            return True
        elif "Duplicate entry" in str(e):
            print("Migration record already exists, continuing...")
            return True
        else:
            print(f"Error: {e}")
            return False

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