package org.xyzh.ai.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.xyzh.common.dto.ai.TbAiConversation;

import java.util.List;

/**
 * @description 对话会话数据访问层
 * @filename AiConversationMapper.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@Mapper
public interface AiConversationMapper extends BaseMapper<TbAiConversation> {

    /**
     * 插入会话
     */
    int insertConversation(TbAiConversation conversation);

    /**
     * 更新会话（动态更新非null字段）
     */
    int updateConversation(TbAiConversation conversation);

    /**
     * 逻辑删除会话
     */
    int deleteConversation(TbAiConversation conversation);

    /**
     * 根据ID查询会话
     */
    TbAiConversation selectConversationById(@org.apache.ibatis.annotations.Param("conversationId") String conversationId);

    /**
     * 根据用户ID查询会话列表
     */
    List<TbAiConversation> selectConversationsByUserId(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("agentId") String agentId
    );

    /**
     * 统计用户的会话数量
     */
    long countUserConversations(@org.apache.ibatis.annotations.Param("userId") String userId);

    /**
     * 分页查询用户会话（支持关键词、日期范围、收藏筛选）
     */
    List<TbAiConversation> selectUserConversationsPage(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("agentId") String agentId,
            @org.apache.ibatis.annotations.Param("keyword") String keyword,
            @org.apache.ibatis.annotations.Param("isFavorite") Boolean isFavorite,
            @org.apache.ibatis.annotations.Param("startDate") java.util.Date startDate,
            @org.apache.ibatis.annotations.Param("endDate") java.util.Date endDate,
            @org.apache.ibatis.annotations.Param("pageParam") org.xyzh.common.core.page.PageParam pageParam
    );

    /**
     * 统计查询条件下的会话数量
     */
    long countUserConversationsWithFilter(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("agentId") String agentId,
            @org.apache.ibatis.annotations.Param("keyword") String keyword,
            @org.apache.ibatis.annotations.Param("isFavorite") Boolean isFavorite,
            @org.apache.ibatis.annotations.Param("startDate") java.util.Date startDate,
            @org.apache.ibatis.annotations.Param("endDate") java.util.Date endDate
    );

    /**
     * 搜索会话（标题和摘要全文搜索）
     */
    List<TbAiConversation> searchConversationsByKeyword(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("keyword") String keyword,
            @org.apache.ibatis.annotations.Param("pageParam") org.xyzh.common.core.page.PageParam pageParam
    );

    /**
     * 统计搜索结果数量
     */
    long countSearchConversations(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("keyword") String keyword
    );

    /**
     * 批量更新会话状态
     */
    int batchUpdateConversations(@org.apache.ibatis.annotations.Param("ids") List<String> ids, @org.apache.ibatis.annotations.Param("deleted") Boolean deleted);

    /**
     * 查询用户最近的会话
     */
    List<TbAiConversation> selectRecentConversations(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("limit") Integer limit
    );

    /**
     * 查询热门会话（按消息数排序）
     */
    List<TbAiConversation> selectPopularConversations(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("limit") Integer limit
    );

    /**
     * 查询过期会话ID列表
     */
    List<String> selectExpiredConversationIds(
            @org.apache.ibatis.annotations.Param("userId") String userId,
            @org.apache.ibatis.annotations.Param("beforeDate") java.util.Date beforeDate
    );

    /**
     * @description 查询对话会话列表
     * @param filter 过滤条件
     * @return List<TbAiConversation> 对话会话列表
     * @author yslg
     * @since 2025-10-15
     */
    List<TbAiConversation> selectAiConversations(TbAiConversation filter);
}
