package com.fs.app.controller;

import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.SmsResponse;
import com.fs.common.utils.SmsVerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 短信验证码Controller
 *
 * @author your-name
 */
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsVerifyCodeUtils smsVerifyCodeUtils;

    /**
     * 发送短信验证码
     *
     * @param phone 手机号
     */
    @GetMapping("/sendCode")
    public AjaxResult sendCode(@RequestParam String phone) {
        SmsResponse response = smsVerifyCodeUtils.sendVerifyCode(phone);
        if (response.isSuccess()) {
            return AjaxResult.success("验证码发送成功");
        }
        return AjaxResult.error("验证码发送失败：" + response.getMessage());
    }

    /**
     * 校验短信验证码
     *
     * @param phone 手机号
     * @param code  验证码
     */
    @GetMapping("/verifyCode")
    public AjaxResult verifyCode(@RequestParam String phone, @RequestParam String code) {
        boolean result = smsVerifyCodeUtils.verifyCode(phone, code);
        if (result) {
            return AjaxResult.success("验证成功");
        }
        return AjaxResult.error("验证码错误或已过期");
    }

    /**
     * 获取距离下次可发送的剩余秒数
     *
     * @param phone 手机号
     */
    @GetMapping("/interval")
    public AjaxResult getInterval(@RequestParam String phone) {
        long remain = smsVerifyCodeUtils.getSendIntervalRemain(phone);
        return AjaxResult.success("remain", remain);
    }
}
