from ...sync_system.strategy import DefaultSyncStrategy
from ...sync_system.config import StrategyConfig, OrphanAction, UpdateDirection
from schemas.contract_settlement.contract_settlement import ContractSettlementResponse


class ContractSettlementSyncStrategy(DefaultSyncStrategy[ContractSettlementResponse]):
    """
    Contract Settlement 同步策略（合同子业务）。
    """

    schema = ContractSettlementResponse

    default_config = StrategyConfig(
        auto_bind=True,
        auto_bind_fields=["contract_id"],
        depend_fields={"contract_id": "contract", "project_id": "project"},
        local_orphan_action=OrphanAction.CREATE_REMOTE,
        remote_orphan_action=OrphanAction.NONE,
        update_direction=UpdateDirection.PUSH,
    )

    def preprocess_compare_payload(self, payload: dict) -> dict:
        plan_nodes = payload.get("plan_node")
        if not isinstance(plan_nodes, list):
            return payload
        payload["plan_node"] = [
            {key: value for key, value in item.items() if key != "is_audit"} if isinstance(item, dict) else item
            for item in plan_nodes
        ]
        return payload