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.TalkSessionVo;
import org.dromara.talk.domain.bo.TalkSessionBo;
import org.dromara.talk.service.ITalkSessionService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 对话会话（管理端）
 *
 * @author Lion Li
 * @date 2026-01-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/talk/admin/session")
public class TalkSessionController extends BaseController {

    private final ITalkSessionService talkSessionService;

    /**
     * 查询对话会话列表
     *
     * @param bo 查询条件对象，包含会话ID、客服ID、用户ID等筛选条件
     * @param pageQuery 分页参数，包含页码和每页数量
     */
    @SaCheckPermission("talk:session:list")
    @GetMapping("/list")
    public TableDataInfo<TalkSessionVo> list(TalkSessionBo bo, PageQuery pageQuery) {
        return talkSessionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出对话会话列表
     *
     * @param bo 查询条件对象，包含会话ID、客服ID、用户ID等筛选条件
     * @param response HTTP响应对象，用于输出Excel文件流
     */
    @SaCheckPermission("talk:session:export")
    @Log(title = "对话会话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TalkSessionBo bo, HttpServletResponse response) {
        List<TalkSessionVo> list = talkSessionService.queryList(bo);
        ExcelUtil.exportExcel(list, "对话会话", TalkSessionVo.class, response);
    }

    /**
     * 获取对话会话详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("talk:session:query")
    @GetMapping("/{id}")
    public R<TalkSessionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(talkSessionService.queryById(id));
    }

    /**
     * 新增对话会话
     *
     * @param bo 会话对象，包含会话ID、客服ID、用户ID、开始时间等信息
     */
    @SaCheckPermission("talk:session:add")
    @Log(title = "对话会话", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TalkSessionBo bo) {
        return toAjax(talkSessionService.insertByBo(bo));
    }

    /**
     * 修改对话会话
     *
     * @param bo 会话对象，包含会话ID及需要修改的字段（结束时间、对话内容等）
     */
    @SaCheckPermission("talk:session:edit")
    @Log(title = "对话会话", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TalkSessionBo bo) {
        return toAjax(talkSessionService.updateByBo(bo));
    }

    /**
     * 删除对话会话
     *
     * @param ids 主键串
     */
    @SaCheckPermission("talk:session:remove")
    @Log(title = "对话会话", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(talkSessionService.deleteWithValidByIds(List.of(ids), true));
    }
}
