"""
AlgoSphere India — Passenger WSGI Entry Point
File: passenger_wsgi  (NO .py extension on cPanel)

CepatCloud / cPanel deployment using Passenger + a2wsgi bridge.
Follows the same pattern as EnglishEval, eduCRM, BusTrack deployments.

CRITICAL NOTES for CepatCloud:
- This file must be named 'passenger_wsgi' (no .py extension)
- Lazy imports ONLY — no top-level imports that might fail at boot
- bcrypt pinned to 4.0.1 in requirements.txt
- DATABASE_URL special chars must be URL-encoded in .env
- .htaccess must have: Options -Indexes + deny .env access
"""
import sys
import os

# ── Path setup ────────────────────────────────────────────────────────────
INTERP = os.path.join(os.environ["HOME"], "virtualenv", "algosphere", "3.12", "bin", "python3")
if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)

APP_DIR = os.path.dirname(os.path.abspath(__file__))
if APP_DIR not in sys.path:
    sys.path.insert(0, APP_DIR)

os.chdir(APP_DIR)

# ── Lazy import + ASGI→WSGI bridge ────────────────────────────────────────
def application(environ, start_response):
    from a2wsgi import ASGIMiddleware
    from app.main import app
    return ASGIMiddleware(app)(environ, start_response)
