package com.sundot.airport.web.controller.system;

import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.service.SmsService;
import com.sundot.airport.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信验证码控制器
 *
 * @author wangxx
 */
@Slf4j
@RestController
@RequestMapping("/sms")
@Api(tags = "短信验证码管理")
public class SmsController {

    @Autowired
    private SmsService smsService;

    /**
     * 发送短信验证码
     *
     * @param phoneNumber 手机号码
     * @return 结果
     */
    @PostMapping("/code")
    @ApiOperation("发送短信验证码")
    public AjaxResult sendSmsCode(@RequestParam String phoneNumber) {
        if (StringUtils.isEmpty(phoneNumber)) {
            return AjaxResult.error("手机号码不能为空");
        }

        try {
            boolean result = smsService.sendSmsCode(phoneNumber);
            if (result) {
                return AjaxResult.success("验证码发送成功，请注意查收短信");
            } else {
                return AjaxResult.error("验证码发送失败");
            }
        } catch (Exception e) {
            log.error("发送短信验证码异常：{}", e.getMessage(), e);
            return AjaxResult.error(e.getMessage());
        }
    }
}
