package edu.whut.domain.trade.service.lock.factory;
import edu.whut.domain.trade.model.entity.GroupBuyActivityEntity;
import edu.whut.domain.trade.model.entity.TradeLockRuleCommandEntity;
import edu.whut.domain.trade.model.entity.TradeLockRuleFilterBackEntity;
import edu.whut.domain.trade.service.lock.filter.ActivityUsabilityRuleFilter;
import edu.whut.domain.trade.service.lock.filter.UserTakeLimitRuleFilter;
import edu.whut.types.design.framework.link.model2.LinkArmory;
import edu.whut.types.design.framework.link.model2.chain.BusinessLinkedList;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * 交易规则过滤链工厂：把各个规则节点按顺序组装成责任链。
 */
@Slf4j
@Service
public class TradeLockRuleFilterFactory {

    /**
     * 通过 Spring @Bean 暴露：外部只需注入 BusinessLinkedList 即可调用 apply
     */
    @Bean("tradeRuleFilter")
    public BusinessLinkedList<TradeLockRuleCommandEntity, DynamicContext, TradeLockRuleFilterBackEntity> tradeRuleFilter(ActivityUsabilityRuleFilter activityUsabilityRuleFilter, UserTakeLimitRuleFilter userTakeLimitRuleFilter) {
        // 1. 组装链
        LinkArmory<TradeLockRuleCommandEntity, DynamicContext, TradeLockRuleFilterBackEntity> linkArmory =
                new LinkArmory<>("交易规则过滤链", activityUsabilityRuleFilter, userTakeLimitRuleFilter);

        // 2. 返回链容器（即可作为责任链使用）
        return linkArmory.getLogicLink();
    }

    /**
     * 动态上下文：在链路中共享数据，避免重复查询。
     */
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class DynamicContext {
        /** 拼团活动信息，供后续节点复用 */
        private GroupBuyActivityEntity groupBuyActivity;

    }

}
