import sys
import os

# 1. Add current directory to sys.path
cwd = os.path.dirname(os.path.abspath(__file__))
if cwd not in sys.path:
    sys.path.insert(0, cwd)

# 2. Add cPanel virtual environment site-packages
user_home = os.path.expanduser('~')
sp_311 = os.path.join(user_home, 'virtualenv', 'tg.big-win.us.cc', '3.11', 'lib', 'python3.11', 'site-packages')
if os.path.exists(sp_311) and sp_311 not in sys.path:
    sys.path.insert(0, sp_311)

# 3. Import Flask application safely
try:
    from app import app as application
except Exception as e:
    import traceback
    err_msg = traceback.format_exc()
    def application(environ, start_response):
        status = '200 OK'
        body = f"<h1>WSGI Launch Error</h1><pre>{err_msg}</pre>".encode('utf-8')
        headers = [('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(body)))]
        start_response(status, headers)
        return [body]
