package com.ylx.message.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.message.domain.dto.MessageDetailDTO;
import com.ylx.message.domain.dto.SendMessageDto;
import com.ylx.message.domain.vo.MessageCategoryVO;
import com.ylx.message.domain.vo.MessageDetailVO;
import com.ylx.message.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/message")
@Api(tags = {"客户端消息"})
@Slf4j
@PreAuthorize("@customerAuth.isCustomer()")
public class MessageController {

    @Resource
    private IMessageService messageService;

    @PutMapping("/read/{id}")
    @ApiOperation("标记单条消息已读")
    public R<Void> markRead(@PathVariable("id") Long id) {
        this.messageService.markAsRead(id);
        return R.ok();
    }

    @GetMapping("/category/list")
    @ApiOperation("获取消息分类卡片列表")
    public R<List<MessageCategoryVO>> getCategorySummaryList() {
        List<MessageCategoryVO> list = messageService.getCategoryCards();
        return R.ok(list);
    }

    @PostMapping("/category/detail/list")
    @ApiOperation("获取指定分类下的消息详情列表")
    public R<Page<MessageDetailVO>> getMessageListByCategory(@Validated @RequestBody MessageDetailDTO dto) {
        Page<MessageDetailVO> data = this.messageService.getCategoryMessages(dto);
        return R.ok(data);
    }

    @Deprecated
    @PostMapping("/sendMessage")
    @ApiOperation("发送消息，用来添加数据的")
    public R<Page<MessageDetailVO>> sendMessage(@Validated @RequestBody SendMessageDto dto) {
        this.messageService.sendMessage(dto.getUserId(), dto.getTriggerEvent(), dto.getVariables());
        return R.ok();
    }
}
