from .app import *
from .app import routes as routes
from .app.routes.worker_routes import worker_bp


def get_hugpy_flask(name=None,allowed_origins=None,debug=False):
    name = name or "hugpy_flask"
    app = get_Flask_app(
        name=name,
        routes=routes,
        allowed_origins=allowed_origins,
        debug=debug
    )
    # Dual-mount the worker/model routes under /api as well.
    #
    # gunicorn serves these at /llm/... and /models; the /api prefix the worker
    # uses exists ONLY because the public nginx (hugpy.ai) strips it. A worker
    # that reaches central directly — e.g. over WireGuard at http://<wg-ip>:7002,
    # bypassing nginx — would 404 on its /api/llm/... calls. Registering the
    # blueprint again under /api makes /api/llm/workers/* and /api/llm/models/*
    # resolve on gunicorn itself, so the direct (proxy-less) route works
    # identically to the nginx route. The original /llm/... mount is untouched.
    try:
        app.register_blueprint(worker_bp, url_prefix="/api", name="worker_bp_api")
    except (ValueError, AssertionError):
        # Idempotent: already mounted on this app instance.
        pass
    return app
