package handler

import (
	"fmt"
	"youngee_b_api/consts"
	"youngee_b_api/middleware"
	"youngee_b_api/model/http_model"
	"youngee_b_api/service"
	"youngee_b_api/util"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	log "github.com/sirupsen/logrus"
)

func WrapUpdateUserInfoHandler(ctx *gin.Context) {
	handler := newUpdateUserInfoHandler(ctx)
	baseRun(handler)
}

func newUpdateUserInfoHandler(ctx *gin.Context) *UpdateUserInfoHandler {
	return &UpdateUserInfoHandler{
		req:  http_model.NewUpdateUserInfoRequest(),
		resp: http_model.NewUpdateUserInfoResponse(),
		ctx:  ctx,
	}
}

type UpdateUserInfoHandler struct {
	req  *http_model.UpdateUserInfoRequest
	resp *http_model.CommonResponse
	ctx  *gin.Context
}

func (h *UpdateUserInfoHandler) getRequest() interface{} {
	return h.req
}
func (h *UpdateUserInfoHandler) getContext() *gin.Context {
	return h.ctx
}
func (h *UpdateUserInfoHandler) getResponse() interface{} {
	return h.resp
}
func (h *UpdateUserInfoHandler) run() {
	auth := middleware.GetSessionAuth(h.ctx)
	data := *&http_model.UpdateUserInfoRequest{}
	data = *h.req
	fmt.Printf("update auth %+v %+v ", auth, data)
	err := service.User.Update(h.ctx, data, auth.ID, auth.EnterpriseID)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[UpdateUserInfoHandler] call FindByID err:%+v\n", err)
		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
		log.Info("UpdateUserInfo fail,req:%+v", h.req)
		return
	}

	h.resp.Message = "更新用户信息成功！"
}
func (h *UpdateUserInfoHandler) checkParam() error {
	return nil
}
