package com.kym.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.kym.common.R;
import com.kym.entity.Message;
import com.kym.entity.queryParams.MessageQueryParams;
import com.kym.entity.vo.MessageVo;
import com.kym.service.MessageService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 消息通知控制器
 *
 * @author skyline
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    private final MessageService messageService;

    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    /**
     * 发送消息
     */
    @PostMapping("/send")
    public R<?> sendMessage(@RequestBody MessageVo vo) {
        messageService.sendMessage(vo);
        return R.success();
    }

    /**
     * 批量发送消息
     */
    @PostMapping("/batchSend")
    public R<?> batchSendMessage(@RequestBody MessageVo vo) {
        messageService.batchSendMessage(vo);
        return R.success();
    }

    /**
     * 分页查询消息列表
     */
    @GetMapping("/list")
    public R<?> listMessage(@ModelAttribute MessageQueryParams params) {
        // 默认查询当前用户的消息
        if (params.getReceiverId() == null) {
            params.setReceiverId(StpUtil.getLoginIdAsLong());
        }
        return R.success(messageService.listMessage(params));
    }

    /**
     * 获取消息详情（自动标记已读）
     */
    @GetMapping("/detail/{messageId}")
    public R<?> getMessageDetail(@PathVariable("messageId") Long messageId) {
        return R.success(messageService.getMessageDetail(messageId));
    }

    /**
     * 标记消息已读
     */
    @PostMapping("/read/{messageId}")
    public R<?> markAsRead(@PathVariable("messageId") Long messageId) {
        messageService.markAsRead(messageId);
        return R.success();
    }

    /**
     * 批量标记已读
     */
    @PostMapping("/batchRead")
    public R<?> batchMarkAsRead(@RequestBody MessageVo vo) {
        messageService.batchMarkAsRead(vo.getIds());
        return R.success();
    }

    /**
     * 全部标记已读
     */
    @PostMapping("/readAll")
    public R<?> markAllAsRead(@RequestParam(required = false) Integer type) {
        Long receiverId = StpUtil.getLoginIdAsLong();
        messageService.markAllAsRead(receiverId, type);
        return R.success();
    }

    /**
     * 删除消息
     */
    @DeleteMapping("/delete/{messageId}")
    public R<?> deleteMessage(@PathVariable("messageId") Long messageId) {
        messageService.deleteMessage(messageId);
        return R.success();
    }

    /**
     * 批量删除消息
     */
    @PostMapping("/batchDelete")
    public R<?> batchDeleteMessage(@RequestBody MessageVo vo) {
        messageService.batchDeleteMessage(vo.getIds());
        return R.success();
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/unreadCount")
    public R<?> getUnreadCount() {
        Long receiverId = StpUtil.getLoginIdAsLong();
        return R.success(messageService.getUnreadCount(receiverId));
    }

    /**
     * 按类型获取未读消息数量
     */
    @GetMapping("/unreadCountByType")
    public R<?> getUnreadCountByType() {
        Long receiverId = StpUtil.getLoginIdAsLong();
        Map<Integer, Integer> result = messageService.getUnreadCountByType(receiverId);
        return R.success(result);
    }

    /**
     * 获取最新未读消息（用于下拉预览）
     */
    @GetMapping("/latestUnread")
    public R<?> getLatestUnread(@RequestParam(defaultValue = "5") int limit) {
        Long receiverId = StpUtil.getLoginIdAsLong();
        List<Message> messages = messageService.getLatestUnread(receiverId, limit);
        return R.success(messages);
    }
}
