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

type WorkspaceNumsHandler struct {
	ctx  *gin.Context
	req  *http_model.WorkspaceNumsRequest
	resp *http_model.CommonResponse
}

func (w WorkspaceNumsHandler) getContext() *gin.Context {
	return w.ctx
}

func (w WorkspaceNumsHandler) getResponse() interface{} {
	return w.resp
}

func (w WorkspaceNumsHandler) getRequest() interface{} {
	return w.req
}

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

func (w WorkspaceNumsHandler) checkParam() error {
	return nil
}

func newWorkspaceNumsHandler(ctx *gin.Context) *WorkspaceNumsHandler {
	return &WorkspaceNumsHandler{
		ctx:  ctx,
		req:  http_model.NewWorkspaceNumsRequest(),
		resp: http_model.NewWorkspaceNumsResponse(),
	}
}
