package com.zioinfo.mall.uiws.message.mapper;

import com.zioinfo.mall.uiws.message.model.UiwsMessage;
import com.zioinfo.mall.uiws.message.model.UiwsMessageRcv;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 쪽지 MyBatis 매퍼. 원본 JPA MessageRepository/MessageRcvRepository → MyBatis 변환 이식.
 * 사용자명은 mall_account(username↔display_name) 조인으로 라벨화(코어 user 재사용, 별도 user 테이블 미이식).
 */
@Mapper
public interface MessageMapper {

    // ── message 헤더
    int insertMessage(UiwsMessage m);

    UiwsMessage findMessageById(@Param("messageId") Long messageId);

    boolean existsMessageById(@Param("messageId") Long messageId);

    /** 보낸쪽지 페이징(SENDER_DEL_YN='N' 제외). */
    List<UiwsMessage> searchSent(@Param("senderId") String senderId,
                                 @Param("from") LocalDateTime from,
                                 @Param("to") LocalDateTime to,
                                 @Param("titleKeyword") String titleKeyword,
                                 @Param("limit") int limit,
                                 @Param("offset") int offset);

    long countSent(@Param("senderId") String senderId,
                   @Param("from") LocalDateTime from,
                   @Param("to") LocalDateTime to,
                   @Param("titleKeyword") String titleKeyword);

    /** 보낸쪽지 다중 소프트삭제(본인 것만). */
    int softDeleteSent(@Param("senderId") String senderId,
                       @Param("ids") List<Long> ids,
                       @Param("actor") String actor);

    // ── 수신자
    int insertRcv(UiwsMessageRcv rcv);

    List<UiwsMessageRcv> findRcvByMessageId(@Param("messageId") Long messageId);

    List<UiwsMessageRcv> findRcvByMessageIds(@Param("ids") List<Long> ids);

    UiwsMessageRcv findRcvByMessageAndReceiver(@Param("messageId") Long messageId,
                                               @Param("receiverId") String receiverId);

    long countUnread(@Param("receiverId") String receiverId);

    /** 받은쪽지 개봉처리(READ_YN='Y', READ_AT). */
    int markRead(@Param("rcvId") Long rcvId, @Param("actor") String actor, @Param("readAt") LocalDateTime readAt);

    int softDeleteReceived(@Param("receiverId") String receiverId,
                           @Param("ids") List<Long> ids,
                           @Param("actor") String actor);

    /** 받은쪽지 목록(조인: message + rcv). 행: messageId,title,senderId,sentAt,readYn. */
    List<Map<String, Object>> searchReceived(@Param("receiverId") String receiverId,
                                             @Param("from") LocalDateTime from,
                                             @Param("to") LocalDateTime to,
                                             @Param("titleKeyword") String titleKeyword,
                                             @Param("limit") int limit,
                                             @Param("offset") int offset);

    long countReceived(@Param("receiverId") String receiverId,
                       @Param("from") LocalDateTime from,
                       @Param("to") LocalDateTime to,
                       @Param("titleKeyword") String titleKeyword);

    // ── 사용자명 라벨(코어 user 재사용)
    List<Map<String, String>> findUserNames(@Param("ids") List<String> ids);
}
