package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"youngee_b_api/consts"
	"youngee_b_api/db"
	"youngee_b_api/middleware"
	"youngee_b_api/model/http_model"
	"youngee_b_api/util"
)

func WrapAddReceiveAddressHandler(ctx *gin.Context) {
	handler := newAddReceiveAddressHandler(ctx)
	baseRun(handler)
}

type AddReceiveAddressHandler struct {
	ctx  *gin.Context
	req  *http_model.AddReceiveAddressRequest
	resp *http_model.CommonResponse
}

func (a AddReceiveAddressHandler) getContext() *gin.Context {
	return a.ctx
}

func (a AddReceiveAddressHandler) getResponse() interface{} {
	return a.resp
}

func (a AddReceiveAddressHandler) getRequest() interface{} {
	return a.req
}

func (a AddReceiveAddressHandler) run() {
	auth := middleware.GetSessionAuth(a.ctx)
	enterpriseID := auth.EnterpriseID
	err := db.AddReceiveAddress(a.ctx, enterpriseID, a.req)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[AddReceiveAddressHandler] call AddReceiveAddress err:%+v\n", err)
		util.HandlerPackErrorResp(a.resp, consts.ErrorInternal, "")
		logrus.Info("AddReceiveAddress fail,req:%+v", a.req)
		return
	}
	a.resp.Message = "新增收货地址成功"
}

func (a AddReceiveAddressHandler) checkParam() error {
	return nil
}

func newAddReceiveAddressHandler(ctx *gin.Context) *AddReceiveAddressHandler {
	return &AddReceiveAddressHandler{
		ctx:  ctx,
		req:  http_model.NewAddReceiveAddressRequest(),
		resp: http_model.NewAddReceiveAddressResponse(),
	}
}
