package waf

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

type CcIpListHandler struct {
	*handler.Handler
	ccIpListService waf.CcIpListService
	wafLogService admin.WafLogService
}

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

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
	}


	go  h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
		Uid:       int(req.Uid),
		RequestIp: ctx.ClientIP(), // 复制 ClientIP
		UserAgent: ctx.Request.UserAgent(), // 复制 UserAgent
		Api:       ctx.Request.URL.Path, // 复制 Path
		HostId: int(req.HostId),
		ExtraData: req,
	})
	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
	}

	go  h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
		Uid:       int(req.Uid),
		RequestIp: ctx.ClientIP(), // 复制 ClientIP
		UserAgent: ctx.Request.UserAgent(), // 复制 UserAgent
		Api:       ctx.Request.URL.Path, // 复制 Path
		HostId: int(req.HostId),
		ExtraData: req,
	})
	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)
}