"""
cPanel Setup Python App — Passenger entry.
Form:
  Application startup file = passenger_wsgi.py
  Application Entry point  = application
"""
from __future__ import annotations

import os
import sys

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

# Load .env
env_path = os.path.join(ROOT, ".env")
if os.path.isfile(env_path):
    with open(env_path, encoding="utf-8", errors="ignore") as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith("#") or "=" not in line:
                continue
            k, v = line.split("=", 1)
            k, v = k.strip(), v.strip().strip('"').strip("'")
            if k and k not in os.environ:
                os.environ[k] = v

from app import app as _fastapi_app

# Passenger expects WSGI callable named `application`
try:
    from a2wsgi import ASGIMiddleware

    application = ASGIMiddleware(_fastapi_app)
except ImportError:
    # fallback if a2wsgi missing (may not work on all hosts)
    application = _fastapi_app
