package com.zhentao.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.entity.MatchmakerChatMessage;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 红娘与用户聊天消息Mapper
 */
@Mapper
public interface MatchmakerChatMessageMapper extends BaseMapper<MatchmakerChatMessage> {

    /**
     * 根据会话ID分页查询消息
     */
    @Select("SELECT * FROM matchmaker_chat_message WHERE conversation_id = #{conversationId} AND is_deleted = 0 ORDER BY send_time DESC")
    IPage<MatchmakerChatMessage> selectByConversationId(Page<MatchmakerChatMessage> page, @Param("conversationId") String conversationId);

    /**
     * 根据消息ID查询
     */
    @Select("SELECT * FROM matchmaker_chat_message WHERE message_id = #{messageId} AND is_deleted = 0")
    MatchmakerChatMessage selectByMessageId(@Param("messageId") String messageId);

    /**
     * 查询红娘的所有会话消息（分页）
     */
    @Select("SELECT * FROM matchmaker_chat_message WHERE matchmaker_id = #{matchmakerId} AND is_deleted = 0 ORDER BY send_time DESC")
    IPage<MatchmakerChatMessage> selectByMatchmakerId(Page<MatchmakerChatMessage> page, @Param("matchmakerId") Long matchmakerId);

    /**
     * 查询用户与红娘的所有会话消息（分页）
     */
    @Select("SELECT * FROM matchmaker_chat_message WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY send_time DESC")
    IPage<MatchmakerChatMessage> selectByUserId(Page<MatchmakerChatMessage> page, @Param("userId") Long userId);

    /**
     * 查询红娘与特定用户的聊天记录（分页）
     */
    @Select("SELECT * FROM matchmaker_chat_message WHERE matchmaker_id = #{matchmakerId} AND user_id = #{userId} AND is_deleted = 0 ORDER BY send_time DESC")
    IPage<MatchmakerChatMessage> selectByMatchmakerAndUser(Page<MatchmakerChatMessage> page, 
                                                           @Param("matchmakerId") Long matchmakerId, 
                                                           @Param("userId") Long userId);

    /**
     * 查询离线消息（用户未读的消息）
     */
    @Select("SELECT * FROM matchmaker_chat_message WHERE user_id = #{userId} AND sender_type = 2 AND send_status < 3 AND is_deleted = 0 ORDER BY send_time ASC LIMIT #{limit}")
    List<MatchmakerChatMessage> selectOfflineMessagesForUser(@Param("userId") Long userId, @Param("limit") int limit);

    /**
     * 查询离线消息（红娘未读的消息）
     */
    @Select("SELECT * FROM matchmaker_chat_message WHERE matchmaker_id = #{matchmakerId} AND sender_type = 1 AND send_status < 3 AND is_deleted = 0 ORDER BY send_time ASC LIMIT #{limit}")
    List<MatchmakerChatMessage> selectOfflineMessagesForMatchmaker(@Param("matchmakerId") Long matchmakerId, @Param("limit") int limit);

    /**
     * 更新消息为已读
     */
    @Update("UPDATE matchmaker_chat_message SET send_status = 3, read_time = NOW(), update_time = NOW() " +
            "WHERE matchmaker_id = #{matchmakerId} AND user_id = #{userId} AND sender_type = #{senderType} AND send_status < 3 AND is_deleted = 0")
    int updateMessagesToRead(@Param("matchmakerId") Long matchmakerId, 
                             @Param("userId") Long userId, 
                             @Param("senderType") Integer senderType);

    /**
     * 撤回消息
     */
    @Update("UPDATE matchmaker_chat_message SET is_recalled = 1, recall_time = NOW(), update_time = NOW() WHERE message_id = #{messageId}")
    int recallMessage(@Param("messageId") String messageId);

    /**
     * 统计红娘今日消息数
     */
    @Select("SELECT COUNT(*) FROM matchmaker_chat_message WHERE matchmaker_id = #{matchmakerId} AND sender_type = 2 AND DATE(send_time) = CURDATE() AND is_deleted = 0")
    int countMatchmakerTodayMessages(@Param("matchmakerId") Long matchmakerId);

    /**
     * 统计红娘与用户的未读消息数
     */
    @Select("SELECT COUNT(*) FROM matchmaker_chat_message WHERE matchmaker_id = #{matchmakerId} AND user_id = #{userId} AND sender_type = #{senderType} AND send_status < 3 AND is_deleted = 0")
    int countUnreadMessages(@Param("matchmakerId") Long matchmakerId, 
                            @Param("userId") Long userId, 
                            @Param("senderType") Integer senderType);
}
