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 CcIpListHandler struct {
	*handler.Handler
	ccIpListService waf.CcIpListService
}

func NewCcIpListHandler(
    handler *handler.Handler,
    ccIpListService waf.CcIpListService,
) *CcIpListHandler {
	return &CcIpListHandler{
		Handler:      handler,
		ccIpListService: ccIpListService,
	}
}

func (h *CcIpListHandler) GetCcIpList(ctx *gin.Context) {
	var req v1.GetCcIpList
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.ccIpListService.GetCcIpList(ctx, req.WebId)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *CcIpListHandler) AddCcIpList(ctx *gin.Context) {
	var req v1.CcIpList
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.ccIpListService.AddCcWhiteIp(ctx, req.WebId,req.NewIp,req.Comment)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)

}

func (h *CcIpListHandler) DelCcIpList(ctx *gin.Context) {
	var req v1.DelCcIpList
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.ccIpListService.DelCcWhiteIp(ctx, req.WebId,req.Ips)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *CcIpListHandler) EditCcIpList(ctx *gin.Context) {
	var req v1.CcIpList
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.ccIpListService.EditCcWhiteIp(ctx, req.WebId,req.OldIp,req.NewIp,req.Comment)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}