package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.CsMessageBo;
import org.dromara.main.domain.vo.CsMessageVo;
import org.dromara.main.service.ICsMessageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/main/message")
public class CsMessageController extends BaseController {

    private final ICsMessageService messageService;

    /**
     * 获取历史消息
     */
    @SaIgnore
    @GetMapping("/history")
    public TableDataInfo<CsMessageVo> history(
        @RequestParam Long sessionId,
        @RequestParam(required = false) Long beforeMsgId,
        PageQuery pageQuery) {
        return messageService.queryHistoryMessages(sessionId, beforeMsgId, pageQuery);
    }

    /**
     * 发送文本消息
     */
    @SaIgnore
    @Log(title = "发送文本消息", businessType = BusinessType.INSERT)
    @PostMapping("/send/text")
    public R<CsMessageVo> sendText(@Validated @RequestBody CsMessageBo bo) {
        Long currentUserId = LoginHelper.getUserId();
        if (currentUserId != null) {
            bo.setSenderId(currentUserId);
        }
        return R.ok(messageService.sendTextMessage(bo));
    }

    /**
     * 发送图片消息
     */
    @SaIgnore
    @Log(title = "发送图片消息", businessType = BusinessType.INSERT)
    @PostMapping("/send/image")
    public R<CsMessageVo> sendImage(
        @RequestParam Long sessionId,
        @RequestParam String msgNo,
        @RequestParam(required = false) Long senderId,
        @RequestParam("file") MultipartFile file) {
        Long currentUserId = LoginHelper.getUserId();
        return R.ok(messageService.sendImageMessage(sessionId, msgNo, currentUserId != null ? currentUserId : senderId, file));
    }

    /**
     * 发送文件消息
     */
    @SaIgnore
    @Log(title = "发送文件消息", businessType = BusinessType.INSERT)
    @PostMapping("/send/file")
    public R<CsMessageVo> sendFile(
        @RequestParam Long sessionId,
        @RequestParam String msgNo,
        @RequestParam(required = false) Long senderId,
        @RequestParam("file") MultipartFile file) {
        Long currentUserId = LoginHelper.getUserId();

        return R.ok(messageService.sendFileMessage(sessionId, msgNo, currentUserId != null ? currentUserId : senderId, file));
    }

    /**
     * 发送岗位卡片
     */
    @Log(title = "发送岗位卡片", businessType = BusinessType.INSERT)
    @PostMapping("/send/job-card")
    public R<CsMessageVo> sendJobCard(@Validated @RequestBody CsMessageBo bo) {
        return R.ok(messageService.sendJobCard(bo));
    }

    /**
     * 标记消息已读
     */
    @SaIgnore
    @PutMapping("/read")
    public R<Void> markAsRead(@RequestBody Map<String, Long> params) {
        return toAjax(messageService.markAsRead(
            params.get("sessionId"),
            params.get("lastReadMsgId")
        ));
    }

    /**
     * 撤回消息
     */
    @Log(title = "撤回消息", businessType = BusinessType.UPDATE)
    @PutMapping("/{msgId}/recall")
    public R<Void> recallMessage(@PathVariable Long msgId) {
        return toAjax(messageService.recallMessage(msgId));
    }
}
