#import imp
#import os
#import sys


#sys.path.insert(0, os.path.dirname(__file__))

#wsgi = imp.load_source('wsgi', 'passenger_wsgi.py')
#application = wsgi.application

import os
import sys
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent
sys.path.insert(0, str(BASE_DIR))

# Load .env BEFORE importing FastAPI app
try:
    from dotenv import load_dotenv
    load_dotenv(BASE_DIR / ".env", override=True)
    load_dotenv(BASE_DIR / "backend" / ".env", override=True)
except Exception:
    # dotenv missing or file issue - will surface as missing env below
    pass

# Hard fail with clear message if still missing
if "MONGO_URL" not in os.environ:
    raise RuntimeError("MONGO_URL missing. Check /api-analytics/.env and permissions.")
if "DB_NAME" not in os.environ:
    raise RuntimeError("DB_NAME missing. Check /api-analytics/.env and permissions.")

from a2wsgi import ASGIMiddleware
from backend.server import app as fastapi_app

application = ASGIMiddleware(fastapi_app)