from app import create_app
from app.config import Config
import os
import ssl
from flask import Flask

try:
    # Validate FFmpeg paths before creating app
    Config.validate_paths()
    app = create_app()
except Exception as e:
    print(f"Error starting application: {str(e)}")
    raise

if __name__ == '__main__':
    cert_path = os.path.join(os.path.dirname(__file__), 'certs', 'cert.pem')
    key_path = os.path.join(os.path.dirname(__file__), 'certs', 'key.pem')
    
    if not os.path.exists(cert_path) or not os.path.exists(key_path):
        print("SSL certificates not found. Please run generate_cert.py first.")
        exit(1)
    
    print("🔐 Fixed HTTPS Server")
    print("📱 Mobile Access URLs:")
    print(f"   • From this computer: https://localhost:5003")
    print(f"   • From mobile/other devices: https://192.168.234.162:5003")
    print(f"   • Test page: https://192.168.234.162:5003/mobile-test")
    print()
    print("🔧 Using simple SSL context that works better with mobile browsers")
    
    # Use the simple tuple format that Flask handles better
    app.run(
        debug=True,
        host='0.0.0.0',
        port=5003,
        ssl_context=(cert_path, key_path),  # Simple tuple format
        threaded=True
    )