package com.ycwl.basic.face.pipeline.stages;

import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
import com.ycwl.basic.pipeline.annotation.StageConfig;
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
import com.ycwl.basic.pipeline.core.StageResult;
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
import com.ycwl.basic.service.pc.processor.BuyStatusProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 处理购买状态Stage
 * 负责更新源文件的购买状态标记
 *
 * 职责:
 * 1. 从context.memberSourceList读取源文件关联列表
 * 2. 从context.freeSourceIds读取免费源文件ID列表
 * 3. 调用buyStatusProcessor.processBuyStatus()更新购买状态
 *
 * 前置条件: context.memberSourceList不为空
 * 业务说明: 购买状态影响前端显示和用户下载权限
 */
@Slf4j
@Component
@StageConfig(
    stageId = "process_buy_status",
    optionalMode = StageOptionalMode.SUPPORT,
    description = "处理源文件购买状态",
    defaultEnabled = true
)
public class ProcessBuyStatusStage extends AbstractPipelineStage<FaceMatchingContext> {

    @Autowired
    private BuyStatusProcessor buyStatusProcessor;

    @Override
    public String getName() {
        return "ProcessBuyStatus";
    }

    @Override
    protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
        // 只有当memberSourceList不为空时才执行
        List<MemberSourceEntity> memberSourceList = context.getMemberSourceList();
        if (memberSourceList == null || memberSourceList.isEmpty()) {
            log.debug("memberSourceList为空，跳过购买状态处理，faceId={}", context.getFaceId());
            return false;
        }
        return true;
    }

    @Override
    protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
        List<MemberSourceEntity> memberSourceEntityList = context.getMemberSourceList();
        List<Long> freeSourceIds = context.getFreeSourceIds();
        Long memberId = context.getFace().getMemberId();
        Long scenicId = context.getFace().getScenicId();
        Long faceId = context.getFaceId();

        // 防御性检查：memberSourceList为空
        if (memberSourceEntityList == null || memberSourceEntityList.isEmpty()) {
            log.debug("memberSourceList为空，跳过购买状态处理，faceId={}", faceId);
            return StageResult.skipped("memberSourceList为空");
        }

        try {
            // 处理购买状态
            buyStatusProcessor.processBuyStatus(
                    memberSourceEntityList,
                    freeSourceIds,
                    memberId,
                    scenicId,
                    faceId);

            log.info("购买状态处理完成: faceId={}, 源文件数={}, 免费数={}",
                    faceId, memberSourceEntityList.size(),
                    freeSourceIds != null ? freeSourceIds.size() : 0);

            return StageResult.success("购买状态处理完成");

        } catch (Exception e) {
            log.error("处理购买状态失败，faceId={}", faceId, e);
            // 购买状态处理失败不影响主流程，返回降级
            return StageResult.degraded("购买状态处理失败: " + e.getMessage());
        }
    }
}
