package com.jeesharp.business.modules.ai.application;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.ai.convert.AiChatSessionConvert;
import com.jeesharp.business.modules.ai.dto.AiChatSessionSaveDTO;
import com.jeesharp.business.modules.ai.entity.AiChatSession;
import com.jeesharp.business.modules.ai.service.AiChatSessionService;
import com.jeesharp.business.modules.ai.vo.AiChatSessionInfoVO;
import com.jeesharp.business.modules.ai.vo.AiChatSessionListVO;
import com.jeesharp.business.modules.ai.vo.AiChatSessionQueryVO;
import com.jeesharp.business.modules.member.service.MemberService;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * AI 客服会话应用服务：后台会话查询、维护及会员展示信息编排。
 *
 * @author jeesharp
 * @since 2026-06-04
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class AiChatSessionApplicationService {

    private final AiChatSessionService aiChatSessionService;
    private final MemberService memberService;

    public AiChatSessionInfoVO get(String id) {
        AiChatSession entity = aiChatSessionService.get(id);
        if (entity == null) {
            return null;
        }
        AiChatSessionInfoVO vo = AiChatSessionConvert.INSTANCE.toInfoVO(entity);
        enrichInfoDisplay(vo);
        return vo;
    }

    public PageRespVO<AiChatSessionListVO> findPage(Page<AiChatSession> page, AiChatSessionQueryVO query) {
        AiChatSessionQueryVO prepared = prepareQuery(query);
        if (prepared == null) {
            return emptyPage(page);
        }
        Page<AiChatSession> result = aiChatSessionService.findPage(page, prepared);
        PageRespVO<AiChatSessionListVO> pageVo = AiChatSessionConvert.INSTANCE.toPageRespVO(result);
        enrichListDisplay(pageVo.getRecords());
        return pageVo;
    }

    @Transactional(readOnly = false)
    public void save(AiChatSessionSaveDTO dto) {
        AiChatSession entity = AiChatSessionConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            aiChatSessionService.createSession(entity);
            return;
        }
        aiChatSessionService.updateSession(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        aiChatSessionService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 管理端会员关键词筛选 → memberIdIn。
     */
    private AiChatSessionQueryVO prepareQuery(AiChatSessionQueryVO query) {
        AiChatSessionQueryVO q = query != null ? query : new AiChatSessionQueryVO();
        if (StrUtil.isNotBlank(q.getMemberKeyword())) {
            List<String> memberIds = memberService.findIdsByKeyword(q.getMemberKeyword().trim());
            q.setMemberKeyword(null);
            if (CollUtil.isEmpty(memberIds)) {
                return null;
            }
            q.setMemberIdIn(memberIds);
        }
        return q;
    }

    private void enrichListDisplay(List<AiChatSessionListVO> rows) {
        if (CollUtil.isEmpty(rows)) {
            return;
        }
        memberService.enrichMemberDisplay(rows);
    }

    private void enrichInfoDisplay(AiChatSessionInfoVO vo) {
        if (vo == null) {
            return;
        }
        memberService.enrichMemberDisplay(vo);
    }

    private static PageRespVO<AiChatSessionListVO> emptyPage(Page<AiChatSession> page) {
        PageRespVO<AiChatSessionListVO> empty = new PageRespVO<>();
        empty.setRecords(Collections.emptyList());
        empty.setTotal(0L);
        empty.setPageNo(page != null ? page.getPageNo() : 1L);
        empty.setPageSize(page != null ? page.getPageSize() : 10L);
        return empty;
    }
}
