#!/usr/bin/env python
"""
Fake Migrations Script
====================

This script applies the migrations as 'fake' since the fields already exist in the database.
"""

import os
import sys
import subprocess

def run_command(command, description=""):
    """Run a shell command and handle errors"""
    print(f"\n{'='*60}")
    print(f"EXECUTING: {description or command}")
    print(f"{'='*60}")
    
    try:
        result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
        if result.stdout:
            print(result.stdout)
        return True, result.stdout
    except subprocess.CalledProcessError as e:
        print(f"ERROR: {e}")
        if e.stdout:
            print(f"STDOUT: {e.stdout}")
        if e.stderr:
            print(f"STDERR: {e.stderr}")
        return False, str(e)

def main():
    print("""
    ╔══════════════════════════════════════════════════════════════╗
    ║                FAKE MIGRATIONS SCRIPT                      ║
    ║                                                              ║
    ║  This script applies the migrations as 'fake' since          ║
    ║  the fields already exist in the database.                   ║
    ╚══════════════════════════════════════════════════════════════╝
    
    """)
    
    # Check if we're in the right directory
    if not os.path.exists('manage.py'):
        print("ERROR: This script must be run from the Django project root directory")
        sys.exit(1)
    
    print("✓ Django project detected")
    
    # Apply migrations as fake
    success, output = run_command(
        "python manage.py migrate users --fake", 
        "Applying users migrations as fake"
    )
    
    if success:
        print("✓ Migrations applied as fake successfully")
    else:
        print("✗ Failed to apply migrations as fake")
        sys.exit(1)
    
    print("\n🎉 MIGRATIONS FIXED! 🎉")
    print("\nThe database schema and Django models are now synchronized.")

if __name__ == "__main__":
    main()
