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 GlobalLimitHandler struct {
	*handler.Handler
	globalLimitService waf.GlobalLimitService
	wafLogService      admin.WafLogService
}

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

//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
	}


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


	// 2. 启动一个新的 goroutine 在后台记录日志
	go  h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
		Uid:        req.Uid,
		RequestIp:  ctx.ClientIP(), // 复制 ClientIP
		UserAgent:  ctx.Request.UserAgent(), // 复制 UserAgent
		Api:        ctx.Request.URL.Path, // 复制 Path
		HostId:     req.HostId,
		ExtraData:  req,
	})
}

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
	}



	go  h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
		Uid:        req.Uid,
		RequestIp:  ctx.ClientIP(), // 复制 ClientIP
		UserAgent:  ctx.Request.UserAgent(), // 复制 UserAgent
		Api:        ctx.Request.URL.Path, // 复制 Path
		HostId:     req.HostId,
		ExtraData:  req,
	})

	v1.HandleSuccess(ctx, nil)
}

