#!/usr/bin/env python3
"""
Multi-language Chatbot Runner
Author: RSK World (https://rskworld.in)
Founder: Molla Samser
Designer & Tester: Rima Khatun
Contact: help@rskworld.in, +91 93305 39277
Year: 2026
Description: Application runner for multi-language chatbot
"""

import os
import sys
from app import app
from config import get_config

def create_app():
    """Create and configure the Flask application"""
    config_class = get_config()
    app.config.from_object(config_class)
    
    # Initialize app with configuration
    config_class.init_app(app)
    
    return app

def main():
    """Main entry point"""
    # Ensure we're in the right directory
    script_dir = os.path.dirname(os.path.abspath(__file__))
    os.chdir(script_dir)
    
    # Create necessary directories
    os.makedirs('logs', exist_ok=True)
    os.makedirs('instance', exist_ok=True)
    
    # Get configuration
    config_class = get_config()
    
    print("=" * 60)
    print("Multi-language Chatbot")
    print("Powered by RSK World (https://rskworld.in)")
    print("Founder: Molla Samser")
    print("Designer & Tester: Rima Khatun")
    print("Contact: help@rskworld.in, +91 93305 39277")
    print("=" * 60)
    print(f"Environment: {os.getenv('FLASK_ENV', 'development')}")
    print(f"Debug Mode: {app.debug}")
    print(f"Host: {config_class.HOST}")
    print(f"Port: {config_class.PORT}")
    print(f"Supported Languages: {', '.join(config_class.SUPPORTED_LANGUAGES)}")
    print("=" * 60)
    
    # Check for required API keys
    if not config_class.OPENAI_API_KEY:
        print("⚠️  WARNING: OpenAI API key not configured!")
        print("   Set OPENAI_API_KEY environment variable for AI responses.")
        print("   The bot will work with rule-based responses only.")
    
    if not config_class.GOOGLE_TRANSLATE_API_KEY:
        print("⚠️  WARNING: Google Translate API key not configured!")
        print("   Set GOOGLE_TRANSLATE_API_KEY environment variable for better translations.")
        print("   Using free Google Translate service (may have limitations).")
    
    print("🚀 Starting Multi-language Chatbot...")
    print(f"📱 Open your browser and visit: http://{config_class.HOST}:{config_class.PORT}")
    print("🌍 Supports multiple languages with automatic detection!")
    print("🔄 Press Ctrl+C to stop the server")
    print("=" * 60)
    
    try:
        app.run(
            host=config_class.HOST,
            port=config_class.PORT,
            debug=config_class.DEBUG
        )
    except KeyboardInterrupt:
        print("\n👋 Shutting down Multi-language Chatbot...")
        print("Thank you for using RSK World's Multi-language Chatbot!")
        sys.exit(0)
    except Exception as e:
        print(f"❌ Error starting server: {e}")
        sys.exit(1)

if __name__ == '__main__':
    main()
