"""
Passenger WSGI entry point for cPanel.

cPanel's "Setup Python App" uses Phusion Passenger, which expects a WSGI
callable named `application`.  FastAPI is ASGI, so we wrap it with `a2wsgi`.

Log any import-time errors to `tmp/passenger.err` so you can read them
from File Manager when something goes wrong.
"""
import os
import sys
import traceback
from pathlib import Path

HERE = Path(__file__).parent.resolve()
if str(HERE) not in sys.path:
    sys.path.insert(0, str(HERE))

os.chdir(HERE)

try:
    from a2wsgi import ASGIMiddleware
    from main import app  # noqa: E402
    application = ASGIMiddleware(app)
except Exception:
    # Log to a file Passenger can show in its "logs" tab
    err_path = HERE / "tmp" / "passenger.err"
    err_path.parent.mkdir(exist_ok=True)
    err_path.write_text(traceback.format_exc(), encoding="utf-8")
    raise
