package cn.reghao.tnb.message.app.controller;

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.message.app.model.po.UserMessage;
import cn.reghao.tnb.message.app.service.UserMessageService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author reghao
 * @date 2024-04-18 15:04:24
 */
@Tag(name = "用户消息接口")
@RestController
//@RequestMapping("/api/user/message")
@RequestMapping("/api/message/user")
public class UserMessageController {
    private final UserMessageService userMessageService;

    public UserMessageController(UserMessageService userMessageService) {
        this.userMessageService = userMessageService;
    }

    @Operation(summary = "获取未读消息数量", description = "N")
    @GetMapping(value = "/unread", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUnreadCount() {
        long loginUser = UserContext.getUserId();
        int total = userMessageService.getUnreadCount(loginUser);
        return WebResult.success(Map.of("total", total));
    }

    @Operation(summary = "获取未读消息列表", description = "N")
    @GetMapping(value = "/unread/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUnreadMessages() {
        long loginUser = UserContext.getUserId();
        List<UserMessage> list = userMessageService.getMessages(loginUser);
        return WebResult.success(list);
    }

    @Operation(summary = "获取消息详情", description = "N")
    @GetMapping(value = "/detail/{messageId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUserMessage(@PathVariable("messageId") Long messageId) {
        UserMessage userMessage = userMessageService.getUserMessage(messageId);
        return WebResult.success(userMessage);
    }
}
