import sys, os

# Standard WSGI entrypoint for Gunicorn, uWSGI, Render, Railway, systemd
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
if CURRENT_DIR not in sys.path:
    sys.path.insert(0, CURRENT_DIR)

_env_path = os.path.join(CURRENT_DIR, ".env")
if os.path.exists(_env_path):
    with open(_env_path, encoding="utf-8") as _fh:
        for _line in _fh:
            _line = _line.strip()
            if _line and not _line.startswith("#") and "=" in _line:
                _k, _v = _line.split("=", 1)
                _v = _v.strip().strip('"').strip("'")
                if _k.strip() and _v:
                    os.environ.setdefault(_k.strip(), _v)

os.environ.setdefault("MX_ALLOW_INSECURE_DEV_STORE", "1")
os.environ.setdefault("MX_APP_VERSION", "4.0.2")

from mx_admin_credential_setup import configure_once
configure_once()

from mx_web_admin import app as application
application.config['SESSION_COOKIE_SECURE'] = True

app = application

if __name__ == "__main__":
    port = int(os.environ.get("PORT", os.environ.get("MX_WEB_ADMIN_PORT", 8898)))
    application.run(host="0.0.0.0", port=port)
