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.mapper.FaceSampleMapper;
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 加载匹配样本实体Stage
 * 负责将sampleListIds对应的样本实体加载到context.faceSamples,供后续Stage使用
 *
 * 职责:
 * 1. 从context.sampleListIds读取匹配到的样本ID列表
 * 2. 调用faceSampleMapper.listByIds()批量加载样本实体
 * 3. 更新context.faceSamples作为样本实体缓存
 *
 * 设计目的:
 * - 避免后续多个Stage重复调用faceSampleMapper.listByIds()
 * - 统一加载时机,提高性能
 * - 为后续筛选Stage提供样本实体数据源
 *
 * 前置条件: context.sampleListIds不为空
 *
 * 应用场景: 自定义匹配场景,在CustomFaceSearchStage之后
 */
@Slf4j
@Component
@StageConfig(
    stageId = "load_matched_samples",
    optionalMode = StageOptionalMode.UNSUPPORT,
    description = "加载匹配样本实体到缓存"
)
public class LoadMatchedSamplesStage extends AbstractPipelineStage<FaceMatchingContext> {

    @Autowired
    private FaceSampleMapper faceSampleMapper;

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

    @Override
    protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
        // 检查sampleListIds是否为空
        List<Long> sampleListIds = context.getSampleListIds();
        if (sampleListIds == null || sampleListIds.isEmpty()) {
            log.debug("sampleListIds为空,跳过加载匹配样本,faceId={}", context.getFaceId());
            return false;
        }
        return true;
    }

    @Override
    protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
        List<Long> sampleListIds = context.getSampleListIds();
        Long faceId = context.getFaceId();

        // 防御性检查：如果sampleListIds为空，直接跳过
        if (sampleListIds == null || sampleListIds.isEmpty()) {
            log.debug("sampleListIds为空,跳过加载匹配样本,faceId={}", faceId);
            return StageResult.skipped("sampleListIds为空");
        }

        try {
            // 批量加载样本实体
            List<FaceSampleEntity> faceSamples = faceSampleMapper.listByIds(sampleListIds);

            if (faceSamples == null || faceSamples.isEmpty()) {
                log.warn("未找到任何匹配样本实体,faceId={}, sampleListIds={}", faceId, sampleListIds);
                return StageResult.skipped("未找到匹配样本实体");
            }

            // 存入context缓存,供后续Stage使用
            context.setFaceSamples(faceSamples);

            log.info("加载匹配样本实体完成: faceId={}, 样本数={}", faceId, faceSamples.size());

            return StageResult.success(String.format("已加载%d个样本实体", faceSamples.size()));

        } catch (Exception e) {
            log.error("加载匹配样本实体失败,faceId={}", faceId, e);
            // 加载失败影响后续流程,返回失败
            return StageResult.failed("加载匹配样本实体失败: " + e.getMessage());
        }
    }
}
