package handler

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

type UdpLimitHandler struct {
	*Handler
	udpLimitService service.UdpLimitService
}

func NewUdpLimitHandler(
	handler *Handler,
	udpLimitService service.UdpLimitService,
) *UdpLimitHandler {
	return &UdpLimitHandler{
		Handler:         handler,
		udpLimitService: udpLimitService,
	}
}

func (h *UdpLimitHandler) GetUdpLimit(ctx *gin.Context) {

}

func (h *UdpLimitHandler) AddUdpLimit(ctx *gin.Context) {
	req := new(v1.GeneralLimitRequireRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.udpLimitService.AddUdpLimit(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)

}

func (h *UdpLimitHandler) EditUdpLimit(ctx *gin.Context) {
	req := new(v1.UdpLimitSendRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	err := h.udpLimitService.UpdateUdpLimit(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *UdpLimitHandler) DeleteUdpLimit(ctx *gin.Context) {
	req := new(v1.UdpLimitDeleteRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.udpLimitService.DeleteUdpLimit(ctx, req.WafUdpLimitId)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}
