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.TalkUserVo;
import org.dromara.talk.domain.bo.TalkUserBo;
import org.dromara.talk.service.ITalkUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 对话用户
 *
 * @author Lion Li
 * @date 2026-01-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/talk/admin/user")
public class TalkUserController extends BaseController {

    private final ITalkUserService talkUserService;

    /**
     * 查询对话用户列表
     *
     * @param bo 查询条件对象，包含用户名、手机号、状态等筛选条件
     * @param pageQuery 分页参数，包含页码和每页数量
     */
    @SaCheckPermission("system:user:list")
    @GetMapping("/list")
    public TableDataInfo<TalkUserVo> list(TalkUserBo bo, PageQuery pageQuery) {
        return talkUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出对话用户列表
     *
     * @param bo 查询条件对象，包含用户名、手机号、状态等筛选条件
     * @param response HTTP响应对象，用于输出Excel文件流
     */
    @SaCheckPermission("system:user:export")
    @Log(title = "对话用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TalkUserBo bo, HttpServletResponse response) {
        List<TalkUserVo> list = talkUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "对话用户", TalkUserVo.class, response);
    }

    /**
     * 获取对话用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:user:query")
    @GetMapping("/{id}")
    public R<TalkUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(talkUserService.queryById(id));
    }

    /**
     * 新增对话用户
     *
     * @param bo 用户对象，包含用户名、密码、手机号、状态等信息
     */
    @SaCheckPermission("system:user:add")
    @Log(title = "对话用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TalkUserBo bo) {
        return toAjax(talkUserService.insertByBo(bo));
    }

    /**
     * 修改对话用户
     *
     * @param bo 用户对象，包含用户ID及需要修改的字段（用户名、密码、手机号、状态等）
     */
    @SaCheckPermission("system:user:edit")
    @Log(title = "对话用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TalkUserBo bo) {
        return toAjax(talkUserService.updateByBo(bo));
    }

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