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 LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.LoginReq
		if err := httpx.Parse(r, &req); err != nil {
			response.Fail(w, err.Error())
			return
		}

		l := auth.NewLoginLogic(r.Context(), svcCtx)
		l.SetRequest(r) // 注入 http.Request 以便取 X-Client-Id 请求头
		resp, err := l.Login(&req)
		if err != nil {
			response.Fail(w, err.Error())
		} else {
			response.OkWithData(w, resp)
		}
	}
}
