package user

import (
	"net/http"

	"git.i2edu.net/i2/i2-bill-api/internal/logic/user"
	"git.i2edu.net/i2/i2-bill-api/internal/svc"
	"git.i2edu.net/i2/i2-bill-api/internal/types"
	"git.i2edu.net/i2/i2-bill-api/internal/utils"

	"git.i2edu.net/i2/go-zero/rest/httpx"
)

func BindMobileHandler(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.BindMobileRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.OkJson(w, utils.ReturnHTTPFail(err))
			return
		}
		uid, skey, err := ctx.GetClaims(r)
		if err != nil {
			httpx.OkJson(w, utils.ReturnHTTPFail(err))
			return
		}
		l := user.NewBindMobileLogic(r.Context(), ctx)
		l.UserId = uid
		l.SessionKey = skey
		resp, err := l.BindMobile(req)
		if err != nil {
			httpx.OkJson(w, utils.ReturnHTTPFail(err))
		} else {
			httpx.OkJson(w, utils.ReturnHTTPSuccess(resp))
		}
	}
}
