#!/usr/bin/env python
"""
Setup script for Yatra Booking Manual Testing System
"""
import os
import sys
import django
from django.conf import settings
from django.core.management import execute_from_command_line

def setup_django():
    """Setup Django environment"""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yatra_testing.settings')
    django.setup()

def run_migrations():
    """Run database migrations"""
    print("🔧 Setting up database...")
    try:
        execute_from_command_line(['manage.py', 'makemigrations', 'testing_app'])
        execute_from_command_line(['manage.py', 'migrate', '--run-syncdb'])
        print("✅ Database setup completed!")
    except Exception as e:
        print(f"⚠️  Database setup warning: {e}")
        print("💡 This might be normal if tables already exist")

def create_superuser_prompt():
    """Prompt to create superuser for admin access"""
    print("\n🔐 Setting up admin access...")
    try:
        from django.contrib.auth.models import User
        if not User.objects.filter(is_superuser=True).exists():
            print("📝 No admin user found. Creating one for you...")
            execute_from_command_line(['manage.py', 'createsuperuser'])
            print("✅ Admin user created!")
        else:
            print("✅ Admin user already exists!")
    except Exception as e:
        print(f"⚠️  Admin setup info: {e}")

def start_server():
    """Start development server"""
    print("\n🚀 Starting Django development server...")
    print("🌐 Access the manual testing interface at: http://127.0.0.1:8000/")
    print("📊 Dashboard: http://127.0.0.1:8000/")
    print("⚙️  Admin Interface: http://127.0.0.1:8000/admin/")
    print("🛑 Press Ctrl+C to stop the server")
    execute_from_command_line(['manage.py', 'runserver'])

if __name__ == '__main__':
    print("🚌 Yatra Booking System - Manual Testing Setup")
    print("=" * 60)
    
    # Check if manage.py exists
    if not os.path.exists('manage.py'):
        print("❌ Error: manage.py not found. Please run this from the project root.")
        sys.exit(1)
    
    setup_django()
    run_migrations()
    create_superuser_prompt()
    start_server()