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.AiChatSession;
import com.jeesharp.business.modules.ai.mapper.AiChatSessionMapper;
import com.jeesharp.business.modules.ai.service.AiChatSessionService;
import com.jeesharp.business.modules.ai.vo.AiChatSessionQueryVO;
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 AiChatSessionServiceImpl extends CrudService<AiChatSessionMapper, AiChatSession, AiChatSessionQueryVO>
        implements AiChatSessionService {

    @Override
    @Transactional(readOnly = false)
    public void createSession(AiChatSession entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateSession(AiChatSession entity) {
        update(entity);
    }

    @Override
    public List<String> findIdsByTitleOrCodeLike(String keyword) {
        if (StrUtil.isBlank(keyword)) {
            return List.of();
        }
        String trimmed = keyword.trim();
        LambdaQueryWrapper<AiChatSession> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AiChatSession::getDelFlag, "0");
        wrapper.and(w -> w.like(AiChatSession::getSessionTitle, trimmed)
            .or().like(AiChatSession::getSessionCode, trimmed));
        return getBaseMapper().selectList(wrapper).stream()
            .map(AiChatSession::getId)
            .filter(StrUtil::isNotBlank)
            .distinct()
            .collect(Collectors.toList());
    }

    @Override
    public Map<String, String> mapSessionLabelByIds(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();
        }
        AiChatSessionQueryVO query = new AiChatSessionQueryVO();
        query.setIdIn(distinctIds);
        Map<String, String> map = new HashMap<>();
        for (AiChatSession session : findList(query)) {
            if (session == null || StrUtil.isBlank(session.getId())) {
                continue;
            }
            map.put(session.getId(), resolveSessionLabel(session));
        }
        return map;
    }

    private static String resolveSessionLabel(AiChatSession session) {
        if (StrUtil.isNotBlank(session.getSessionTitle())) {
            return session.getSessionTitle();
        }
        return StrUtil.blankToDefault(session.getSessionCode(), session.getId());
    }
}
