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.VideoRecreationHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 处理视频重切Stage
 * 负责触发视频重新切片处理
 *
 * 职责:
 * 1. 从context读取必要参数(scenicId, memberSourceList, faceId, memberId, sampleListIds, isNew)
 * 2. 调用videoRecreationHandler.handleVideoRecreation()触发视频重切
 *
 * 前置条件: context.memberSourceList不为空
 * 业务说明: 视频重切用于根据人脸识别结果重新生成个性化视频片段
 */
@Slf4j
@Component
@StageConfig(
    stageId = "handle_video_recreation",
    optionalMode = StageOptionalMode.SUPPORT,
    description = "处理视频重切逻辑",
    defaultEnabled = true
)
public class HandleVideoRecreationStage extends AbstractPipelineStage<FaceMatchingContext> {

    @Autowired
    private VideoRecreationHandler videoRecreationHandler;

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

    @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) {
        Long scenicId = context.getFace().getScenicId();
        List<MemberSourceEntity> memberSourceEntityList = context.getMemberSourceList();
        Long faceId = context.getFaceId();
        Long memberId = context.getFace().getMemberId();
        List<Long> sampleListIds = context.getSampleListIds();
        boolean isNew = context.isNew();

        // 防御性检查：memberSourceList为空
        if (memberSourceEntityList == null || memberSourceEntityList.isEmpty()) {
            log.debug("memberSourceList为空，跳过视频重切，faceId={}", faceId);
            return StageResult.skipped("memberSourceList为空");
        }

        try {
            // 处理视频重切
            videoRecreationHandler.handleVideoRecreation(
                    scenicId,
                    memberSourceEntityList,
                    faceId,
                    memberId,
                    sampleListIds,
                    isNew);

            log.info("视频重切处理完成: faceId={}, scenicId={}, 源文件数={}",
                    faceId, scenicId, memberSourceEntityList.size());

            return StageResult.success("视频重切处理完成");

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