// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package auth

import (
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
	"sundynix-micro-go/app/auth/api/internal/logic/auth"
	"sundynix-micro-go/app/auth/api/internal/svc"
	"sundynix-micro-go/app/auth/api/internal/types"
	"sundynix-micro-go/common/response"
)

// 微信小程序登录
func MiniLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.MiniLoginReq
		if err := httpx.Parse(r, &req); err != nil {
			response.Fail(w, err.Error())
			return
		}

		// 从请求头读取 clientId，优先级高于 body
		// 前端统一在请求头设置 X-Client-Id: plant_mini / radio_mini 等
		if clientId := r.Header.Get("X-Client-Id"); clientId != "" {
			req.ClientId = clientId
		}

		l := auth.NewMiniLoginLogic(r.Context(), svcCtx)
		resp, err := l.MiniLogin(&req)
		if err != nil {
			response.Fail(w, err.Error())
		} else {
			response.OkWithData(w, resp)
		}
	}
}
