package org.dromara.chat.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.chat.domain.bo.ChatMessageBo;
import org.dromara.chat.domain.vo.ChatMessageVo;
import org.dromara.chat.service.ChatMessageService;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
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.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天消息控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat/message")
public class ChatMessageController extends BaseController {

    private final ChatMessageService chatMessageService;

    /**
     * 查询聊天消息列表
     */
    @SaCheckPermission("chat:message:list")
    @GetMapping("/list")
    public TableDataInfo<ChatMessageVo> list(ChatMessageBo bo, PageQuery pageQuery) {
        return chatMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聊天消息列表
     */
    @SaCheckPermission("chat:message:export")
    @Log(title = "聊天消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChatMessageBo bo, HttpServletResponse response) {
        List<ChatMessageVo> list = chatMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "聊天消息", ChatMessageVo.class, response);
    }

    /**
     * 获取聊天消息详细信息
     */
    @SaCheckPermission("chat:message:query")
    @GetMapping("/{id}")
    public R<ChatMessageVo> getInfo(@PathVariable Long id) {
        return R.ok(chatMessageService.queryById(id));
    }

    /**
     * 新增聊天消息
     */
    @SaCheckPermission("chat:message:add")
    @Log(title = "聊天消息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ChatMessageBo bo) {
        return toAjax(chatMessageService.insertByBo(bo));
    }

    /**
     * 修改聊天消息
     */
    @SaCheckPermission("chat:message:edit")
    @Log(title = "聊天消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ChatMessageBo bo) {
        return toAjax(chatMessageService.updateByBo(bo));
    }

    /**
     * 删除聊天消息
     */
    @SaCheckPermission("chat:message:remove")
    @Log(title = "聊天消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(chatMessageService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据会话ID查询消息列表
     */
    @SaCheckPermission("chat:message:query")
    @GetMapping("/session/{sessionId}")
    public R<List<ChatMessageVo>> getBySessionId(@PathVariable Long sessionId) {
        return R.ok(chatMessageService.queryListBySessionId(sessionId));
    }

    /**
     * 根据用户ID查询消息列表
     */
    @SaCheckPermission("chat:message:query")
    @GetMapping("/user/{userId}")
    public R<List<ChatMessageVo>> getByUserId(@PathVariable Long userId) {
        return R.ok(chatMessageService.queryListByUserId(userId));
    }

    /**
     * 根据会话ID和用户ID查询消息列表
     */
    @SaCheckPermission("chat:message:query")
    @GetMapping("/session/{sessionId}/user/{userId}")
    public R<List<ChatMessageVo>> getBySessionIdAndUserId(@PathVariable Long sessionId, @PathVariable Long userId) {
        return R.ok(chatMessageService.queryListBySessionIdAndUserId(sessionId, userId));
    }

}