from __future__ import annotations

from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix

from .config import BASE_DIR, SECRET_KEY
from .db import init_db
from .routes import bp


def create_app() -> Flask:
    app = Flask(
        __name__,
        template_folder=str(BASE_DIR / "templates"),
        static_folder=str(BASE_DIR / "static"),
    )
    app.secret_key = SECRET_KEY
    # Trust 2 proxy hops: NPM → internal nginx → gunicorn
    app.wsgi_app = ProxyFix(app.wsgi_app, x_for=2, x_proto=1, x_host=1)
    app.register_blueprint(bp)
    init_db()
    return app


app = create_app()
