package org.qinan.safetyeval.adapter.web;

import cn.hutool.json.JSONUtil;
import com.jjb.saas.message.client.message.facede.MessageFacade;
import com.jjb.saas.message.client.message.request.MessageSendCmd;
import com.jjb.saas.message.client.message.request.MessageTargetCmd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.qinan.safetyeval.client.api.AccountApi;
import org.qinan.safetyeval.client.co.AccountCO;
import org.qinan.safetyeval.client.dto.*;
import org.qinan.safetyeval.domain.exception.BizException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static org.qinan.safetyeval.domain.exception.ErrorCode.SMS_SOURCE_CODE_NOT_EXIST;

/**
 * 用户适配层（Controller）
 *
 * @author safety-eval
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/safetyEval/account")
public class AccountController {

    @Resource
    private AccountApi accountApi;

    @DubboReference
    private MessageFacade messageFacade;

    @Value("${sms.sign.sourceCode:}")
    private String sourceCode;

    @ApiOperation("新增用户")
    @PostMapping("/save")
    public SingleResponse<AccountCO> add(@Validated @RequestBody AccountAddCmd cmd) {
        return accountApi.add(cmd);
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/get")
    public SingleResponse<AccountCO> get(@ApiParam("主键ID") @RequestParam Long id) {
        return accountApi.get(id);
    }

    @ApiOperation("修改用户")
    @PostMapping("/modify")
    public SingleResponse<AccountCO> modify(@Validated @RequestBody AccountModifyCmd cmd) {
        return accountApi.modify(cmd);
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public SingleResponse<Void> delete(@ApiParam("主键ID") @RequestParam Long id) {
        return accountApi.delete(id);
    }

    @ApiOperation("分页查询用户")
    @GetMapping("/page")
    public PageResponse<AccountCO> page(@Validated AccountPageQuery query) {
        return accountApi.page(query);
    }

    @ApiOperation("发送短信")
    @PostMapping("/sendMessage")
    public com.alibaba.cola.dto.SingleResponse<Boolean> sendMessage(@Validated @RequestBody SendMessageCmd cmd) {
        if (StringUtils.isBlank(sourceCode)) {
            throw new BizException(SMS_SOURCE_CODE_NOT_EXIST);
        }

        MessageSendCmd messageSendCmd = new MessageSendCmd();
        messageSendCmd.setBusinessId(UUID.randomUUID().toString().replace("-", ""));

        MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
        messageTargetCmd.setMobile(cmd.getMobile());
        messageSendCmd.setTargetCmd(messageTargetCmd);
        messageSendCmd.setNeedTokenEnum(false);

        messageSendCmd.setSourceCode(sourceCode);
        Map<String, Object> codeMap = new HashMap<>();
        codeMap.put("valid_code", "123456");
        messageSendCmd.setParams(codeMap);

        log.info("发送短信参数: messageSendCmd: {}", JSONUtil.toJsonStr(messageSendCmd));
        com.alibaba.cola.dto.SingleResponse<Boolean> result = messageFacade.send(messageSendCmd);
        log.info("发送短信结果: result: {}", JSONUtil.toJsonStr(result));
        return result;
    }

    @ApiOperation("用户同步gbs")
    @PostMapping("/syncUserToGBS")
    public SingleResponse<AccountCO> syncUserToGBS(@ApiParam("账号") @RequestParam String account) {
        return accountApi.syncUserToGBS(account);
    }

}
