"""
Phusion Passenger entrypoint for cPanel "Setup Python App".

cPanel's Application Manager looks for a callable named `application` here at the
application root. We load the .env early so HUSTLERS_ENV can select the settings
module (development -> dev, anything else -> prod), then hand off to Django.
Environment variables live in your home .env (e.g. /home/jopexco/.env).
"""
import os
import sys
from pathlib import Path

APP_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, APP_DIR)

# Load the first .env we find so HUSTLERS_ENV / DB creds are available now.
try:
    import environ

    for _candidate in [
        os.environ.get('ENV_FILE', ''),
        os.path.join(APP_DIR, '.env'),
        str(Path.home() / '.env'),
    ]:
        if _candidate and os.path.exists(_candidate):
            environ.Env.read_env(_candidate)
            break
except Exception:
    pass

_env = os.environ.get('HUSTLERS_ENV', '').lower()
os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE',
    'config.settings.dev' if _env in ('development', 'dev', 'local')
    else 'config.settings.prod',
)

from config.wsgi import application  # noqa: E402,F401
