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.WhatsAppUserVo;
import org.dromara.talk.domain.bo.WhatsAppUserBo;
import org.dromara.talk.service.IWhatsAppUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * WhatsApp用户
 *
 * @author Lion Li
 * @date 2026-02-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/talk/admin/whatsapp/user")
public class WhatsAppUserController extends BaseController {

    private final IWhatsAppUserService whatsAppUserService;

    /**
     * 查询WhatsApp用户列表
     *
     * @param bo 查询条件对象，包含用户账号等筛选条件
     * @param pageQuery 分页参数，包含页码和每页数量
     */
    @SaCheckPermission("talk:whatsapp:user:list")
    @GetMapping("/list")
    public TableDataInfo<WhatsAppUserVo> list(WhatsAppUserBo bo, PageQuery pageQuery) {
        return whatsAppUserService.queryPageList(bo, pageQuery);
    }

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

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

    /**
     * 新增WhatsApp用户
     *
     * @param bo 用户对象，包含用户账号等信息
     */
    @SaCheckPermission("talk:whatsapp:user:add")
    @Log(title = "WhatsApp用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WhatsAppUserBo bo) {
        return toAjax(whatsAppUserService.insertByBo(bo));
    }

    /**
     * 修改WhatsApp用户
     *
     * @param bo 用户对象，包含用户ID及需要修改的字段（用户账号等）
     */
    @SaCheckPermission("talk:whatsapp:user:edit")
    @Log(title = "WhatsApp用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WhatsAppUserBo bo) {
        return toAjax(whatsAppUserService.updateByBo(bo));
    }

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