package org.qinan.safetyeval.adapter.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.qinan.safetyeval.client.api.AccountApi;
import org.qinan.safetyeval.client.api.OrgInfoApi;
import org.qinan.safetyeval.client.co.AccountCO;
import org.qinan.safetyeval.client.co.OrgInfoCO;
import org.qinan.safetyeval.client.dto.AccountAddCmd;
import org.qinan.safetyeval.client.dto.OrgInfoAddCmd;
import org.qinan.safetyeval.client.dto.SendMessageCmd;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.infrastructure.remote.SmsRemote;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 对外开放接口（免鉴权）。
 * <p>
 * 所有不需要登录验证的接口统一挂载在 /safetyEval/images 前缀下。
 * </p>
 */
@Api(tags = "对外开放接口")
@RestController
@RequestMapping("/safetyEval/images")
public class ImagesOpenController {

    @Resource
    private SmsRemote smsRemote;

    @Resource
    private AccountApi accountApi;

    @Resource
    private OrgInfoApi orgInfoApi;

    @ApiOperation("发送短信")
    @PostMapping("/sendMessage")
    public com.alibaba.cola.dto.SingleResponse<Boolean> sendMessage(@Validated @RequestBody SendMessageCmd cmd) {
        return smsRemote.sign(cmd.getMobile());
    }

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

    @ApiOperation("新增机构信息（注册）")
    @PostMapping("/org-info/save")
    public SingleResponse<OrgInfoCO> orgInfoSave(@Validated @RequestBody OrgInfoAddCmd cmd) {
        return orgInfoApi.add(cmd);
    }

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

    @ApiOperation("发送短信")
    @PostMapping("/test")
    public SingleResponse test() {
        return SingleResponse.success("hello word");
    }
}
