package com.zanxiang.game.module.manage.controller;

import com.zanxiang.erp.security.annotation.PreAuthorize;
import com.zanxiang.game.module.manage.pojo.params.KfApiParam;
import com.zanxiang.game.module.manage.pojo.params.KfUserUpdateParam;
import com.zanxiang.game.module.manage.pojo.vo.KfGameVO;
import com.zanxiang.game.module.manage.service.IKfMsgService;
import com.zanxiang.game.module.manage.service.IKfUserService;
import com.zanxiang.module.util.JsonUtil;
import com.zanxiang.module.util.pojo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.util.function.Tuple2;

import java.util.List;

/**
 * @author : lingfeng
 * @time : 2023-11-29
 * @description : 客服系统接口
 */
@Api(tags = {"客服系统接口"})
@RestController
@RequestMapping("/kf")
@Slf4j
public class KfMsgController {

    @Autowired
    private IKfMsgService kfMsgService;

    @Autowired
    private IKfUserService kfUserService;

    @ApiOperation(value = "发送图片消息 (参数按照发送消息的参数给, 外加图片文件)")
    @PostMapping(value = "/upload/msg")
    @PreAuthorize(permissionKey = "manage:kf:uploadMsg")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Tuple2.class)})
    public ResultVO<Tuple2<String, String>> uploadMsg(String param, MultipartFile files) {
        return ResultVO.ok(kfMsgService.uploadMsg(JsonUtil.toObj(param, KfApiParam.class), files));
    }

    @ApiOperation(value = "小游戏列表查询")
    @GetMapping(value = "/game/list")
    @PreAuthorize(permissionKey = "manage:kf:gameList")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = KfGameVO.class)})
    public ResultVO<List<KfGameVO>> list() {
        return ResultVO.ok(kfMsgService.getKfGameList());
    }

    @ApiOperation(value = "客服接口通用api")
    @PostMapping(value = "/comm/api")
    @PreAuthorize(permissionKey = "manage:kf:commApi")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = String.class)})
    public ResultVO<String> list(@Validated @RequestBody KfApiParam param) {
        return ResultVO.ok(kfMsgService.kfApi(param));
    }

    @ApiOperation(value = "获取小游戏授权码")
    @GetMapping(value = "/auth/code")
    @PreAuthorize(permissionKey = "manage:kf:authCode")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = KfGameVO.class)})
    public ResultVO<String> getAuthCode(@RequestParam String appId) {
        return ResultVO.ok(kfUserService.getAuthCode(appId));
    }

    @ApiOperation(value = "授权信息回调")
    @PostMapping(value = "/user/update")
    @PreAuthorize(permissionKey = "manage:kf:userUpdate")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = String.class)})
    public ResultVO<Boolean> kfUserUpdate(@Validated @RequestBody KfUserUpdateParam param) {
        return ResultVO.ok(kfUserService.kfUserUpdate(param));
    }
}
