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

type OperateReceiveAddress struct {
	ctx  *gin.Context
	req  *http_model.OperateReceiveAddressRequest
	resp *http_model.CommonResponse
}

func (o OperateReceiveAddress) getContext() *gin.Context {
	return o.ctx
}

func (o OperateReceiveAddress) getResponse() interface{} {
	return o.resp
}

func (o OperateReceiveAddress) getRequest() interface{} {
	return o.req
}

func (o OperateReceiveAddress) run() {
	auth := middleware.GetSessionAuth(o.ctx)
	enterpriseID := auth.EnterpriseID
	err := db.OperateReceiveAddress(o.ctx, enterpriseID, o.req)
	if err != nil {
		// 数据库查询失败，返回5001
		logrus.Errorf("[OperateReceiveAddress] call OperateReceiveAddress err:%+v\n", err)
		util.HandlerPackErrorResp(o.resp, consts.ErrorInternal, "")
		logrus.Info("OperateReceiveAddress fail,req:%+v", o.req)
		return
	}
	if o.req.OperateType == 2 {
		o.resp.Message = "已删除该地址信息"
	} else {
		o.resp.Message = "修改地址信息成功"
	}
}

func (o OperateReceiveAddress) checkParam() error {
	return nil
}

func newOperateReceiveAddressHandler(ctx *gin.Context) *OperateReceiveAddress {
	return &OperateReceiveAddress{
		ctx:  ctx,
		req:  http_model.NewOperateReceiveAddressRequest(),
		resp: http_model.NewOperateReceiveAddressResponse(),
	}
}
