package acquirer

import (
	"net/http"

	"git.i2edu.net/i2/i2-bill-api/internal/logic/acquirer"
	"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 EnrollHandler(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		uid, skey, err := ctx.GetClaims(r)
		if err != nil {
			httpx.OkJson(w, utils.ReturnHTTPFail(err))
			return
		}
		var req types.EnrollRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}

		l := acquirer.NewEnrollLogic(r.Context(), ctx)
		l.UserId = uid
		l.SessionKey = skey
		resp, err := l.Enroll(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}
