"""
策略管理相关的API端点。
"""
from flask import Blueprint, jsonify, abort

from core.app_state import get_trader, logger

strategy_bp = Blueprint('strategy_routes', __name__, url_prefix='/yu')

@strategy_bp.route("/clear/<strategy_name>", methods=["DELETE"])
def clear_strategy_route(strategy_name: str): # Renamed
    """清除指定策略的持仓管理数据。"""
    logger.info(f"接收到清除策略持仓请求: {strategy_name}")
    try:
        trader = get_trader()
        if not trader:
            logger.error("Trader instance not available for clear_strategy request.")
            return jsonify({"success": False, "message": "Trader not available"}), 503

        # 假设 clear_position_manager 是 BaseTrader 或其子类的方法
        if hasattr(trader, 'clear_position_manager'):
            cleared = trader.clear_position_manager(strategy_name)
            if cleared:
                logger.info(f"策略 {strategy_name} 持仓数据已清除。")
                return jsonify({"success": True, "message": f"Strategy {strategy_name} cleared successfully."}), 200
            else:
                logger.warning(f"尝试清除一个不存在的策略或清除失败: {strategy_name}")
                return jsonify({"success": False, "message": f"Strategy {strategy_name} not found or could not be cleared."}), 404
        else:
            logger.error(f"Trader instance does not have 'clear_position_manager' method for strategy {strategy_name}")
            return jsonify({"success": False, "message": "Clear position manager feature not available."}), 501 # Not Implemented

    except Exception as e:
        logger.error(f"清除策略 {strategy_name} 持仓时出错: {str(e)}", exc_info=True)
        abort(500, description=f"服务器内部错误，清除策略 {strategy_name} 持仓失败。") 