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 WrapGetReceiveAddressHandler(ctx *gin.Context) {
	handler := newGetReceiveAddressHandler(ctx)
	baseRun(handler)
}

type GetReceiveAddressHandler struct {
	ctx  *gin.Context
	req  *http_model.GetReceiveAddressRequest
	resp *http_model.CommonResponse
}

func (g GetReceiveAddressHandler) getContext() *gin.Context {
	return g.ctx
}

func (g GetReceiveAddressHandler) getResponse() interface{} {
	return g.resp
}

func (g GetReceiveAddressHandler) getRequest() interface{} {
	return g.req
}

func (g GetReceiveAddressHandler) run() {
	auth := middleware.GetSessionAuth(g.ctx)
	enterpriseID := auth.EnterpriseID
	data, err := db.GetReceiveAddress(g.ctx, enterpriseID)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[GetReceiveAddressHandler] call GetReceiveAddress err:%+v\n", err)
		util.HandlerPackErrorResp(g.resp, consts.ErrorInternal, "")
		logrus.Info("GetReceiveAddress fail,req:%+v", g.req)
		return
	}
	g.resp.Data = data
}

func (g GetReceiveAddressHandler) checkParam() error {
	return nil
}

func newGetReceiveAddressHandler(ctx *gin.Context) *GetReceiveAddressHandler {
	return &GetReceiveAddressHandler{
		ctx:  ctx,
		req:  http_model.NewGetReceiveAddressRequest(),
		resp: http_model.NewGetReceiveAddressResponse(),
	}
}
