package org.dromara.im.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.im.domain.bo.ImFriendBo;
import org.dromara.im.domain.vo.ImFriendVo;
import org.dromara.im.service.IImFriendService;
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.common.mybatis.core.page.TableDataInfo;

/**
 * 好友
 *
 * @author Luo.J
 * @date 2025-05-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/friend")
public class ImFriendController extends BaseController {

    private final IImFriendService imFriendService;

    /**
     * 查询好友列表
     */
    @SaCheckPermission("system:friend:list")
    @GetMapping("/list")
    public TableDataInfo<ImFriendVo> list(ImFriendBo bo, PageQuery pageQuery) {
        return imFriendService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出好友列表
     */
    @SaCheckPermission("system:friend:export")
    @Log(title = "好友", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ImFriendBo bo, HttpServletResponse response) {
        List<ImFriendVo> list = imFriendService.queryList(bo);
        ExcelUtil.exportExcel(list, "好友", ImFriendVo.class, response);
    }

    /**
     * 获取好友详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:friend:query")
    @GetMapping("/{id}")
    public R<ImFriendVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(imFriendService.queryById(id));
    }

    /**
     * 新增好友
     */
    @SaCheckPermission("system:friend:add")
    @Log(title = "好友", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImFriendBo bo) {
        return toAjax(imFriendService.insertByBo(bo));
    }

    /**
     * 修改好友
     */
    @SaCheckPermission("system:friend:edit")
    @Log(title = "好友", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImFriendBo bo) {
        return toAjax(imFriendService.updateByBo(bo));
    }

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

    /**
     * 初始化好友列表
     */
    @SaIgnore
    @GetMapping("/initialize")
    public R<Void> initialize(@RequestParam(required = false) Long userId) {
        imFriendService.initialize(userId);
        return R.ok("好友列表初始化成功！");
    }
}
