"""
WSGI entry point — Apache mod_wsgi deployment.

Usage:
  Apache config:
    WSGIDaemonProcess financials python-home=/var/www/html/financials/.venv
    WSGIScriptAlias / /var/www/html/financials/wsgi.py

  Local development (preferred):
    export FLASK_APP=wsgi.py FLASK_DEBUG=1
    export DATABASE_URL=sqlite:///financials.db
    flask run

  Behind a reverse proxy at a sub-path (e.g. Apache proxying /financials → :5000):
    export URL_PREFIX=/financials
    flask run
"""
import os
from app import create_app

application = create_app()

# When served behind a reverse proxy at a sub-path, set SCRIPT_NAME so
# Flask's url_for() generates correct URLs including the prefix.
# Apache already strips the prefix from PATH_INFO before forwarding —
# this middleware only sets SCRIPT_NAME so link generation works.
_prefix = os.environ.get('URL_PREFIX', '').rstrip('/')
if _prefix:
    _inner = application.wsgi_app

    def _script_name_wsgi(environ, start_response):
        environ['SCRIPT_NAME'] = _prefix
        return _inner(environ, start_response)

    application.wsgi_app = _script_name_wsgi
