package com.infoepoch.pms.agent.domain.care.orchestrator;

import com.infoepoch.pms.agent.common.utils.LogHelper;
import com.infoepoch.pms.agent.domain.care.log.CareTraceLogSupport;
import com.infoepoch.pms.agent.domain.care.model.CareConversationState;
import com.infoepoch.pms.agent.domain.care.model.CareQuery;
import com.infoepoch.pms.agent.domain.care.model.CareTaskType;
import com.infoepoch.pms.agent.domain.care.state.CareConversationStateService;
import com.infoepoch.pms.agent.domain.care.understanding.CareQueryUnderstandingService;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

/**
 * 推荐编排入口
 */
@Component
public class CareRecommendationOrchestrator {

    private final CareQueryUnderstandingService understandingService;
    private final CareConversationStateService conversationStateService;
    private final ActivityRecommendationExecutor activityRecommendationExecutor;
    private final UserRecommendationExecutor userRecommendationExecutor;
    private final GeneralConversationExecutor generalConversationExecutor;

    public CareRecommendationOrchestrator(CareQueryUnderstandingService understandingService,
                                          CareConversationStateService conversationStateService,
                                          ActivityRecommendationExecutor activityRecommendationExecutor,
                                          UserRecommendationExecutor userRecommendationExecutor,
                                          GeneralConversationExecutor generalConversationExecutor) {
        this.understandingService = understandingService;
        this.conversationStateService = conversationStateService;
        this.activityRecommendationExecutor = activityRecommendationExecutor;
        this.userRecommendationExecutor = userRecommendationExecutor;
        this.generalConversationExecutor = generalConversationExecutor;
    }

    /**
     * 统一编排推荐请求：理解用户意图后分发到具体执行器。
     */
    public Flux<String> stream(String traceId, String sessionId, String message) {
        CareConversationState currentState = conversationStateService.load(sessionId).orElse(null);
        CareQuery query = understandingService.understand(traceId, sessionId, message, currentState);
        LogHelper.info(this, CareTraceLogSupport.format(
                traceId,
                sessionId,
                "路由分发结果",
                "任务类型=%s，是否续轮=%s".formatted(
                        toChineseTaskType(query.taskType()),
                        query.continuation()
                )
        ));
        if (query.taskType() == CareTaskType.GENERAL_CHAT) {
            return generalConversationExecutor.execute(traceId, sessionId, query);
        }
        if (query.taskType() == CareTaskType.RECOMMEND_USERS) {
            return userRecommendationExecutor.execute(traceId, sessionId, query, currentState);
        }
        return activityRecommendationExecutor.execute(traceId, sessionId, query, currentState);
    }

    /**
     * 将任务类型转换成中文展示。
     */
    private String toChineseTaskType(CareTaskType taskType) {
        if (taskType == null) {
            return "未知";
        }
        return switch (taskType) {
            case RECOMMEND_ACTIVITIES -> "推荐活动";
            case RECOMMEND_USERS -> "推荐用户";
            case GENERAL_CHAT -> "通用对话";
        };
    }
}
