package org.hzhub.controller.chat;

import java.util.List;

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

/**
 * 会话管理
 *
 * @author ageerle
 * @date 2025-12-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/session")
public class ChatSessionController extends BaseController {

    private final IChatSessionService chatSessionService;

    /**
     * 查询会话管理列表
     */
    @SaCheckPermission("system:session:list")
    @GetMapping("/list")
    public TableDataInfo<ChatSessionVo> list(ChatSessionBo bo, PageQuery pageQuery) {
        if(!LoginHelper.isLogin()){
            // 如果用户没有登录,返回空会话列表
            return TableDataInfo.build();
        }
        // 默认查询当前用户会话
        bo.setUserId(LoginHelper.getUserId());
        return chatSessionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会话管理列表
     */
    @SaCheckPermission("system:session:export")
    @Log(title = "会话管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChatSessionBo bo, HttpServletResponse response) {
        List<ChatSessionVo> list = chatSessionService.queryList(bo);
        ExcelUtil.exportExcel(list, "会话管理", ChatSessionVo.class, response);
    }

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

    /**
     * 新增会话管理
     */
    @SaCheckPermission("system:session:add")
    @Log(title = "会话管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody ChatSessionBo bo) {
        bo.setUserId(LoginHelper.getUserId());
        chatSessionService.insertByBo(bo);
        // 返回会话id
        return R.ok(bo.getId());
    }

    /**
     * 修改会话管理
     */
    @SaCheckPermission("system:session:edit")
    @Log(title = "会话管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChatSessionBo bo) {
        return toAjax(chatSessionService.updateByBo(bo));
    }

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