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 TcpLimitHandler struct {
	*Handler
	tcpLimitService service.TcpLimitService
}

func NewTcpLimitHandler(
	handler *Handler,
	tcpLimitService service.TcpLimitService,
) *TcpLimitHandler {
	return &TcpLimitHandler{
		Handler:         handler,
		tcpLimitService: tcpLimitService,
	}
}

func (h *TcpLimitHandler) GetTcpLimit(ctx *gin.Context) {

}

func (h *TcpLimitHandler) AddTcpLimit(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.tcpLimitService.AddTcpLimit(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)

}

func (h *TcpLimitHandler) EditTcpLimit(ctx *gin.Context) {
	req := new(v1.TcpLimitSendRequest)

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

}

func (h *TcpLimitHandler) DeleteTcpLimit(ctx *gin.Context) {
	req := new(v1.TcpLimitDeleteRequest)

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

}
