"""注册 GET /api/instance/dashboard(三所共用)."""
from __future__ import annotations

from typing import Any, Callable, Optional

from flask import Flask, jsonify


def register_instance_dashboard_routes(
    app: Flask,
    *,
    login_required: Callable,
    get_db: Callable,
    fetch_options_positions: Optional[Callable[[], list[dict[str, Any]]]] = None,
    hedge_enabled: bool = False,
) -> None:
    from lib.instance.instance_dashboard_lib import build_instance_dashboard_payload

    @app.route("/api/instance/dashboard")
    @login_required
    def api_instance_dashboard():
        conn = get_db()
        try:
            payload = build_instance_dashboard_payload(
                conn,
                fetch_options_positions=fetch_options_positions,
                hedge_enabled=bool(hedge_enabled),
            )
            return jsonify(payload)
        finally:
            conn.close()
