package com.jeesharp.business.modules.ai.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.ai.entity.AiChatMessage;
import com.jeesharp.business.modules.ai.mapper.AiChatMessageMapper;
import com.jeesharp.business.modules.ai.service.AiChatMessageService;
import com.jeesharp.business.modules.ai.vo.AiChatMessageQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * AI 会话消息领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-04
 */
@Service
@Transactional(readOnly = true)
public class AiChatMessageServiceImpl extends CrudService<AiChatMessageMapper, AiChatMessage, AiChatMessageQueryVO>
        implements AiChatMessageService {

    private static final int MESSAGE_PREVIEW_MAX_LEN = 80;

    @Override
    @Transactional(readOnly = false)
    public void createMessage(AiChatMessage entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateMessage(AiChatMessage entity) {
        update(entity);
    }

    @Override
    public List<String> findIdsByContentLike(String keyword) {
        if (StrUtil.isBlank(keyword)) {
            return List.of();
        }
        LambdaQueryWrapper<AiChatMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AiChatMessage::getDelFlag, "0");
        wrapper.like(AiChatMessage::getMessageContent, keyword.trim());
        return getBaseMapper().selectList(wrapper).stream()
            .map(AiChatMessage::getId)
            .filter(StrUtil::isNotBlank)
            .distinct()
            .collect(Collectors.toList());
    }

    @Override
    public Map<String, String> mapMessageContentPreviewByIds(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        List<String> distinctIds = ids.stream().filter(StrUtil::isNotBlank).distinct().toList();
        if (distinctIds.isEmpty()) {
            return Collections.emptyMap();
        }
        AiChatMessageQueryVO query = new AiChatMessageQueryVO();
        query.setIdIn(distinctIds);
        Map<String, String> map = new HashMap<>();
        for (AiChatMessage message : findList(query)) {
            if (message == null || StrUtil.isBlank(message.getId())) {
                continue;
            }
            map.put(message.getId(), truncatePreview(message.getMessageContent()));
        }
        return map;
    }

    private static String truncatePreview(String content) {
        if (StrUtil.isBlank(content)) {
            return "";
        }
        String trimmed = content.trim();
        if (trimmed.length() <= MESSAGE_PREVIEW_MAX_LEN) {
            return trimmed;
        }
        return trimmed.substring(0, MESSAGE_PREVIEW_MAX_LEN) + "…";
    }
}
