#!/usr/bin/env python
"""
Simple migration runner
Runs makemigrations and migrate with noinput flag
"""

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

def setup_django():
    """Setup Django environment"""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
    django.setup()

def run_migrations():
    """Run migrations with noinput flag"""
    print("=== RUNNING MIGRATIONS ===")
    
    try:
        # Run makemigrations with noinput
        print("  Running makemigrations --noinput...")
        execute_from_command_line(['manage.py', 'makemigrations', '--noinput'])
        print("    ✓ makemigrations completed")
        
        # Run migrate with noinput
        print("  Running migrate --noinput...")
        execute_from_command_line(['manage.py', 'migrate', '--noinput'])
        print("    ✓ migrate completed")
        
        return True
        
    except Exception as e:
        print(f"    ✗ Migration failed: {e}")
        return False

def main():
    """Main function"""
    print("=== SIMPLE MIGRATION RUNNER ===")
    print("Running makemigrations and migrate with noinput flag.\n")
    
    try:
        setup_django()
        
        # Run migrations
        if run_migrations():
            print("\n=== MIGRATIONS COMPLETE ===")
            print("✓ All migrations have been applied successfully!")
            print("✓ Database is up to date")
            print("\n🎉 Your application is ready!")
            return True
        else:
            print("\n=== MIGRATIONS FAILED ===")
            print("⚠ Migration issues occurred")
            return False
        
    except Exception as e:
        print(f"\n❌ Migration failed: {e}")
        import traceback
        traceback.print_exc()
        return False

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