#!/usr/bin/env python3
"""
Setup script for Chatterbox TTS Application
This script helps configure the application for different environments.
"""

import os
import shutil
from pathlib import Path

def setup_environment():
    """Setup environment configuration files"""
    project_root = Path(__file__).parent
    
    print("🔧 Setting up Chatterbox TTS Application...")
    
    # Create .env file if it doesn't exist
    env_file = project_root / ".env"
    env_example = project_root / ".env.example"
    
    if not env_file.exists() and env_example.exists():
        print("📝 Creating .env file from .env.example...")
        shutil.copy(env_example, env_file)
        
        # Update PROJECT_ROOT in .env to current directory
        with open(env_file, 'r') as f:
            content = f.read()
        
        content = content.replace('/path/to/your/chatterbox-ui', str(project_root))
        
        with open(env_file, 'w') as f:
            f.write(content)
        
        print(f"✅ Created .env file with PROJECT_ROOT set to: {project_root}")
    else:
        print("ℹ️  .env file already exists")
    
    # Setup backend .env
    backend_env = project_root / "backend" / ".env"
    backend_env_example = project_root / "backend" / ".env.example"
    
    if not backend_env.exists() and backend_env_example.exists():
        print("📝 Creating backend/.env file...")
        shutil.copy(backend_env_example, backend_env)
        
        # Update PROJECT_ROOT in backend .env
        with open(backend_env, 'r') as f:
            content = f.read()
        
        content = content.replace('/Users/stwhite/CODE/chatterbox-ui', str(project_root))
        
        with open(backend_env, 'w') as f:
            f.write(content)
        
        print(f"✅ Created backend/.env file")
    
    # Setup frontend .env
    frontend_env = project_root / "frontend" / ".env"
    frontend_env_example = project_root / "frontend" / ".env.example"
    
    if not frontend_env.exists() and frontend_env_example.exists():
        print("📝 Creating frontend/.env file...")
        shutil.copy(frontend_env_example, frontend_env)
        print(f"✅ Created frontend/.env file")
    
    # Create necessary directories
    directories = [
        project_root / "speaker_data" / "speaker_samples",
        project_root / "tts_temp_outputs",
        project_root / "backend" / "tts_generated_dialogs"
    ]
    
    for directory in directories:
        directory.mkdir(parents=True, exist_ok=True)
        print(f"📁 Created directory: {directory}")
    
    print("\n🎉 Setup complete!")
    print("\n📋 Next steps:")
    print("1. Review and adjust the .env files as needed")
    print("2. Install backend dependencies: cd backend && pip install -r requirements.txt")
    print("3. Start backend server: cd backend && python start_server.py")
    print("4. Start frontend server: cd frontend && python start_dev_server.py")
    print("5. Open http://127.0.0.1:8001 in your browser")

if __name__ == "__main__":
    setup_environment()
