package controllers

import (
	"github.com/gogf/gf/v2/net/ghttp"
	"yx-dataset-server/app/bll"
	"yx-dataset-server/app/errors"
	"yx-dataset-server/app/schema"
	"yx-dataset-server/library/gplus"
)

type VerifyCode struct {
	VerifyCodeBll bll.IVerifyCode
}

func NewVerifyCode(bVerifyCode bll.IVerifyCode) *VerifyCode {
	return &VerifyCode{
		VerifyCodeBll: bVerifyCode,
	}
}

// Send 发送验证码
// @Router POST /web/v1/verify_code/send
func (a *VerifyCode) Send(r *ghttp.Request) {
	req := schema.VerifyCodeRequest{}
	if err := gplus.ParseJson(r, &req); err != nil {
		ResError(r, err)
		return
	}
	ctx := gplus.NewContext(r)
	item, err := a.VerifyCodeBll.ALiYunSendSms(ctx, req)
	if err != nil {
		ResError(r, err)
		return
	}

	ResSuccess(r, item)
}

// Check 检查验证码
// @Summary 查询指定数据
// @Param body schema.VerifyCodeCheck true "验证码校验请求"
// @Success 200 schema.HTTPStatus "{status:OK}"
// @Failure 401 schema.HTTPError "{code:0,message:未授权}"
// @Failure 404 schema.HTTPError "{code:0,message:资源不存在}"
// @Failure 500 schema.HTTPError "{code:0,message:服务器错误}"
// @Router GET /web/v1/verify_code/{id}
func (a *VerifyCode) Check(r *ghttp.Request) {
	req := schema.VerifyCodeCheck{}

	if err := gplus.ParseJson(r, &req); err != nil {
		ResError(r, err)
		return
	}
	ctx := gplus.NewContext(r)
	b, err := a.VerifyCodeBll.Check(ctx, req)
	if err != nil {
		ResError(r, err)
		return
	}
	if !b {
		ResError(r, errors.New400Response("验证码错误"))
		return
	}

	ResSuccess(r, nil)
}
