package com.njcn.msgpush.module.push.controller.admin.message;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.msgpush.framework.common.pojo.CommonResult;
import com.njcn.msgpush.module.push.controller.admin.message.vo.MessageRecordReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;
import com.njcn.msgpush.module.push.dal.dataobject.message.MessageRecordDO;
import com.njcn.msgpush.module.push.service.message.MessageRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.njcn.msgpush.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 消息")
@Slf4j
@RestController
@RequestMapping("/push/message")
public class MessageRecordController {

    @Autowired
    private MessageRecordService messageRecordService;

    @PermitAll
    @PostMapping("/send")
    @Operation(summary = "消息推送")
    @Parameter(name = "reqVOList", description = "消息列表")
    public CommonResult<Boolean> send(@Valid @RequestBody List<MessageRecordReqVO> reqVOList) {
        Boolean result = messageRecordService.send(reqVOList);
        return CommonResult.success(result);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询渠道服务商列表")
    @PreAuthorize("@ss.hasPermission('push:message:page')")
    @Parameter(name = "reqVO", description = "分页查询参数", required = true)
    public CommonResult<Page<MessageRecordDO>> pageChannelProviderConfig(@Validated @RequestBody MessageRecordReqVO reqVO) {
        Page<MessageRecordDO> res = messageRecordService.getPage(reqVO);
        return success(res);
    }

    @PostMapping("/add")
    @Operation(summary = "添加消息记录")
    @PreAuthorize("@ss.hasPermission('push:message:add')")
    @Parameter(name = "reqVO", description = "添加参数", required = true)
    public CommonResult<Boolean> add(@Validated @RequestBody MessageRecordReqVO reqVO) {
        return messageRecordService.add(reqVO) ? success(true) : success(false);
    }

    @PostMapping("/update")
    @Operation(summary = "更新消息记录")
    @PreAuthorize("@ss.hasPermission('push:message:update')")
    @Parameter(name = "reqVO", description = "更新参数", required = true)
    public CommonResult<Boolean> update(@Validated @RequestBody MessageRecordReqVO reqVO) {
        return messageRecordService.update(reqVO) ? success(true) : success(false);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除消息记录")
    @PreAuthorize("@ss.hasPermission('push:message:delete')")
    @Parameter(name = "ids", description = "编号", required = true)
    public CommonResult<Boolean> delete(@RequestBody List<String> ids) {
        return messageRecordService.delete(ids) ? success(true) : success(false);
    }
}
