package com.red.circle.external.adapter.app.sms;

import com.red.circle.external.infra.common.PhoneSms;
import com.red.circle.external.inner.model.cmd.message.SendSmsCmd;
import com.red.circle.framework.web.controller.BaseController;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
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.RestController;

/**
 * 短信消息.
 *
 * @author pengliang on 2023/8/14
 * @eo.api-type http
 * @eo.groupName 消息服务.短信消息
 * @eo.path /external/sms
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/sms", produces = MediaType.APPLICATION_JSON_VALUE)
public class SmsRestController extends BaseController {

  private final PhoneSms phoneSms;

  /**
   * 发送验证码.
   *
   * @eo.name 发送验证码.
   * @eo.url /code
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/code")
  public void sendCode(@RequestBody @Validated SendSmsCmd cmd) {
    phoneSms.sendCode(cmd.getPhonePrefix(), cmd.getPhoneNumber());
  }

  /**
   * 验证验证码.
   *
   * @eo.name 验证验证码.
   * @eo.url /code/check
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/code/check")
  public Boolean checkCode(@RequestBody @Validated SendSmsCmd cmd) {
    return phoneSms.checkCode(cmd.getPhonePrefix(), cmd.getPhoneNumber(), cmd.getCode());
  }

}
