package auth

import (
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"

	"wechat-api/internal/logic/auth"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
)

// swagger:route post /user/login_by_email auth LoginByEmail
//
// Log in by email | 邮箱登录
//
// Log in by email | 邮箱登录
//
// Parameters:
//  + name: body
//    require: true
//    in: body
//    type: LoginByEmailReq
//
// Responses:
//  200: LoginResp

func LoginByEmailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.LoginByEmailReq
		if err := httpx.Parse(r, &req, true); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := auth.NewLoginByEmailLogic(r.Context(), svcCtx, w)
		resp, err := l.LoginByEmail(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
