package org.dromara.talk.controller.admin;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.talk.domain.vo.WhatsAppHistoryVo;
import org.dromara.talk.domain.bo.WhatsAppHistoryBo;
import org.dromara.talk.service.IWhatsAppHistoryService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * WhatsApp历史记录
 *
 * @author Lion Li
 * @date 2026-02-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/talk/admin/whatsapp/history")
public class WhatsAppHistoryController extends BaseController {

    private final IWhatsAppHistoryService whatsAppHistoryService;

    /**
     * 查询WhatsApp历史记录列表
     *
     * @param bo 查询条件对象，包含会话ID、用户账号等筛选条件
     * @param pageQuery 分页参数，包含页码和每页数量
     */
    @SaCheckPermission("talk:whatsapp:history:list")
    @GetMapping("/list")
    public TableDataInfo<WhatsAppHistoryVo> list(WhatsAppHistoryBo bo, PageQuery pageQuery) {
        return whatsAppHistoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出WhatsApp历史记录列表
     *
     * @param bo 查询条件对象，包含会话ID、用户账号等筛选条件
     * @param response HTTP响应对象，用于输出Excel文件流
     */
    @SaCheckPermission("talk:whatsapp:history:export")
    @Log(title = "WhatsApp历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WhatsAppHistoryBo bo, HttpServletResponse response) {
        List<WhatsAppHistoryVo> list = whatsAppHistoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "WhatsApp历史记录", WhatsAppHistoryVo.class, response);
    }

    /**
     * 获取WhatsApp历史记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("talk:whatsapp:history:query")
    @GetMapping("/{id}")
    public R<WhatsAppHistoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(whatsAppHistoryService.queryById(id));
    }

    /**
     * 新增WhatsApp历史记录
     *
     * @param bo 历史记录对象，包含会话ID、用户账号、对话JSON、开始时间、结束时间等信息
     */
    @SaCheckPermission("talk:whatsapp:history:add")
    @Log(title = "WhatsApp历史记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WhatsAppHistoryBo bo) {
        return toAjax(whatsAppHistoryService.insertByBo(bo));
    }

    /**
     * 修改WhatsApp历史记录
     *
     * @param bo 历史记录对象，包含记录ID及需要修改的字段（对话JSON、结束时间等）
     */
    @SaCheckPermission("talk:whatsapp:history:edit")
    @Log(title = "WhatsApp历史记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WhatsAppHistoryBo bo) {
        return toAjax(whatsAppHistoryService.updateByBo(bo));
    }

    /**
     * 删除WhatsApp历史记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("talk:whatsapp:history:remove")
    @Log(title = "WhatsApp历史记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(whatsAppHistoryService.deleteWithValidByIds(List.of(ids), true));
    }
}
