from pathlib import Path

from dotenv import load_dotenv

from env_loader import use_utf8_environment

# Load .env before importing anything from the application: config values
# and runtime paths (DATA_DIR) must be in the environment by the time app
# modules are imported and create_app() runs. Every entry point
# (passenger_wsgi.py, run_local.py) goes through this module.
# utf-8-sig: a .env saved with a UTF-8 BOM (e.g. by Windows editors) would
# otherwise turn its first key into "﻿FLASK_ENV" and silently select DevConfig.
# lswsgi has no LANG (ASCII filesystem encoding): without this, a non-ASCII .env value raises UnicodeEncodeError in load_dotenv.
use_utf8_environment()
load_dotenv(Path(__file__).with_name(".env"), encoding="utf-8-sig")

from modules.core.app_factory import create_app  # noqa: E402

# Passenger
app = create_app()
