package com.m2pool.chat.controller;

import com.m2pool.chat.dto.ChatMessageDto;
import com.m2pool.chat.service.ChatMessageService;
import com.m2pool.chat.vo.MessagePageVo;
import com.m2pool.common.core.Result.R;
import com.m2pool.common.security.annotation.RequiresLogin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
@Api(tags = "聊天消息相关接口")
public class ChatMessageController {

    @Autowired
    private ChatMessageService chatMessageService;

    /**
     * 查询7天前的历史记录
     * @return
     */
    @PostMapping("/find/history/message")
    @ApiOperation(value = "查询7天前的历史记录（废弃，请使用/find/recently/message）", notes = "根据ID查询7天前的历史记录")
    @RequiresLogin
    public R<List<ChatMessageDto>> findHistoryMessage(@RequestBody MessagePageVo pageVo) {
        return chatMessageService.findHistoryMessage(pageVo.getId(), pageVo.getPageNum(), pageVo.getRoomId());
    }

    @PostMapping("/find/recently/message")
    @ApiOperation(value = "查询七天内的记录", notes = "根据ID查询七天内的记录")
    public R<List<ChatMessageDto>> findRecentlyMessage(@RequestBody MessagePageVo pageVo) {
        return chatMessageService.findRecentlyMessage(pageVo.getEmail(),pageVo.getId(), pageVo.getPageNum(), pageVo.getRoomId());
    }

    @GetMapping("/delete/message")
    @ApiOperation(value = "消息撤回或删除（废弃）")
    @RequiresLogin
    public R<String> deleteMessage(
            @ApiParam(value = "消息id", required = true, example = "1")
            @RequestParam(defaultValue = "1") Long id,
            @ApiParam(value = "消息类型 0 文本 1 图片", example = "0")
            @RequestParam(required = false,defaultValue = "0")Integer type
    ) {
        return chatMessageService.deleteMessage(id,type);
    }

    @PostMapping("/read/message")
    @ApiOperation(value = "聊天室消息改已读")
    public R<String> readMessage(@RequestBody MessagePageVo pageVo) {
        return chatMessageService.readMessage(pageVo.getRoomId(),pageVo.getUserType());
    }
}
