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

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 用户问题理解结果。
 *
 * @param taskType 当前输入归属的任务类型
 * @param requestedCount 本轮希望返回的推荐数量
 * @param continuation 当前输入是否属于“再来一些”等续轮请求
 * @param originalMessage 用户本轮原始输入
 * @param activityIntent 从输入中识别出的活动语义或活动名称
 * @param preferenceSummary 从输入中提取的偏好摘要
 * @param searchConditions 可直接用于工具检索的筛选条件
 * @param directReply 预留的直接回复内容
 */
public record CareQuery(
        CareTaskType taskType,
        int requestedCount,
        boolean continuation,
        String originalMessage,
        String activityIntent,
        String preferenceSummary,
        Map<String, Object> searchConditions,
        String directReply
) {

    public CareQuery {
        searchConditions = sanitize(searchConditions);
        activityIntent = activityIntent == null ? "" : activityIntent.trim();
        preferenceSummary = preferenceSummary == null ? "" : preferenceSummary.trim();
        directReply = directReply == null ? "" : directReply.trim();
    }

    /**
     * 清洗筛选条件，仅保留非空键值对。
     */
    private static Map<String, Object> sanitize(Map<String, Object> searchConditions) {
        if (searchConditions == null || searchConditions.isEmpty()) {
            return Collections.emptyMap();
        }
        LinkedHashMap<String, Object> sanitized = new LinkedHashMap<>();
        searchConditions.forEach((key, value) -> {
            if (key != null && value != null) {
                sanitized.put(key, value);
            }
        });
        return Map.copyOf(sanitized);
    }
}
