package waf

import (
	"github.com/gin-gonic/gin"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	"github.com/go-nunu/nunu-layout-advanced/internal/handler"
	"github.com/go-nunu/nunu-layout-advanced/internal/service/api/waf"
	"net/http"
)

type GlobalLimitHandler struct {
	*handler.Handler
	globalLimitService waf.GlobalLimitService
}

func NewGlobalLimitHandler(
	handler *handler.Handler,
	globalLimitService waf.GlobalLimitService,
) *GlobalLimitHandler {
	return &GlobalLimitHandler{
		Handler:            handler,
		globalLimitService: globalLimitService,
	}
}

//func (h *GlobalLimitHandler) GetGlobalLimit(ctx *gin.Context) {
//
//}

func (h *GlobalLimitHandler) AddGlobalLimit(ctx *gin.Context) {
	req := new(v1.GlobalLimitRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.globalLimitService.AddGlobalLimit(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *GlobalLimitHandler) EditGlobalLimit(ctx *gin.Context) {
	req := new(v1.GlobalLimitRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.globalLimitService.EditGlobalLimit(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *GlobalLimitHandler) DeleteGlobalLimit(ctx *gin.Context) {
	req := new(v1.GlobalLimitRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.globalLimitService.DeleteGlobalLimit(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

