from __future__ import annotations

from typing import Any, Dict, List

from core.tool import tool, tool_func


@tool("rule_filter", "规则过滤")
class RuleFilterTool:
    @tool_func(
        {
            "type": "object",
            "properties": {
                "payload": {"type": "object"},
            },
            "required": ["payload"],
        }
    )
    def run(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        raise NotImplementedError("Subclasses must implement run")


@tool("lufa_party_rule_filter_tool", "LUFA 当事人与支付主体规则过滤")
class LufaPartyRuleFilterTool(RuleFilterTool):
    @tool_func(
        {
            "type": "object",
            "properties": {
                "payload": {"type": "object"},
            },
            "required": ["payload"],
        }
    )
    def run(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        # rules = payload.get("rules") or []
        # segment_idx = int(payload.get("segment_idx", 0))
        # total_segments = int(payload.get("total_segments", 0))

        # if not rules or total_segments <= 0:
        #     payload["rules"] = rules
        #     return payload

        # # 奇数分段时将中间段归入前半段
        # first_half_count = (total_segments + 1) // 2

        # if segment_idx < first_half_count:
        #     filtered_rules: List[Dict[str, Any]] = [
        #         r for r in rules if "支付主体审查" not in str(r.get("title", ""))
        #     ]
        # else:
        #     filtered_rules = [
        #         r for r in rules if "当事人审查" not in str(r.get("title", ""))
        #     ]

        # payload["rules"] = filtered_rules
        return payload
