#!/usr/bin/env python3
"""
Development setup script for VFX Project Management System
"""
import os
import subprocess
import sys
from pathlib import Path

def run_command(command, cwd=None):
    """Run a command and return the result"""
    try:
        result = subprocess.run(command, shell=True, cwd=cwd, check=True, capture_output=True, text=True)
        return True, result.stdout
    except subprocess.CalledProcessError as e:
        return False, e.stderr

def setup_backend():
    """Set up the backend environment"""
    print("Setting up backend...")
    backend_dir = Path("backend")
    
    # Check if virtual environment exists
    venv_dir = backend_dir / "venv"
    if not venv_dir.exists():
        print("Creating virtual environment...")
        success, output = run_command("python -m venv venv", cwd=backend_dir)
        if not success:
            print(f"Failed to create virtual environment: {output}")
            return False
    
    # Install dependencies
    print("Installing backend dependencies...")
    if os.name == 'nt':  # Windows
        pip_cmd = "venv\\Scripts\\pip install -r requirements.txt"
    else:  # Unix-like
        pip_cmd = "venv/bin/pip install -r requirements.txt"
    
    success, output = run_command(pip_cmd, cwd=backend_dir)
    if not success:
        print(f"Failed to install backend dependencies: {output}")
        return False
    
    # Copy .env file if it doesn't exist
    env_file = backend_dir / ".env"
    env_example = backend_dir / ".env.example"
    if not env_file.exists() and env_example.exists():
        print("Creating .env file from example...")
        env_file.write_text(env_example.read_text())
    
    print("Backend setup complete!")
    return True

def setup_frontend():
    """Set up the frontend environment"""
    print("Setting up frontend...")
    frontend_dir = Path("frontend")
    
    # Install dependencies
    print("Installing frontend dependencies...")
    success, output = run_command("npm install", cwd=frontend_dir)
    if not success:
        print(f"Failed to install frontend dependencies: {output}")
        return False
    
    # Copy .env file if it doesn't exist
    env_file = frontend_dir / ".env"
    env_example = frontend_dir / ".env.example"
    if not env_file.exists() and env_example.exists():
        print("Creating .env file from example...")
        env_file.write_text(env_example.read_text())
    
    print("Frontend setup complete!")
    return True

def main():
    """Main setup function"""
    print("VFX Project Management System - Development Setup")
    print("=" * 50)
    
    # Check if we're in the right directory
    if not Path("backend").exists() or not Path("frontend").exists():
        print("Error: Please run this script from the project root directory")
        sys.exit(1)
    
    # Setup backend
    if not setup_backend():
        print("Backend setup failed!")
        sys.exit(1)
    
    # Setup frontend
    if not setup_frontend():
        print("Frontend setup failed!")
        sys.exit(1)
    
    print("\n" + "=" * 50)
    print("Setup complete! To start development:")
    print("\n1. Start the backend:")
    print("   cd backend")
    if os.name == 'nt':
        print("   venv\\Scripts\\activate")
    else:
        print("   source venv/bin/activate")
    print("   uvicorn main:app --reload")
    print("\n2. Start the frontend (in a new terminal):")
    print("   cd frontend")
    print("   npm run dev")
    print("\n3. Access the application at http://localhost:5173")
    print("   API documentation at http://localhost:8000/docs")

if __name__ == "__main__":
    main()