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"
	"github.com/mcuadros/go-defaults"
	"net/http"
)

type UdpForWardingHandler struct {
	*handler.Handler
	udpForWardingService waf.UdpForWardingService
	wafLogService admin.WafLogService
}

func NewUdpForWardingHandler(
	handler *handler.Handler,
	udpForWardingService waf.UdpForWardingService,
	wafLogService admin.WafLogService,
) *UdpForWardingHandler {
	return &UdpForWardingHandler{
		Handler:              handler,
		udpForWardingService: udpForWardingService,
		wafLogService:        wafLogService,
	}
}

func (h *UdpForWardingHandler) GetUdpForWarding(ctx *gin.Context) {
	req := new(v1.GetForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.udpForWardingService.GetUdpForWarding(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *UdpForWardingHandler) AddUdpForWarding(ctx *gin.Context) {
	req := new(v1.UdpForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	id, err := h.udpForWardingService.AddUdpForwarding(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,
		RuleId:     id,
		ExtraData:  req,
	})
	v1.HandleSuccess(ctx, nil)

}

func (h *UdpForWardingHandler) EditUdpForWarding(ctx *gin.Context) {
	req := new(v1.UdpForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	err := h.udpForWardingService.EditUdpForwarding(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,
		RuleId:     req.UdpForwardingData.Id,
		ExtraData:  req,
	})
	v1.HandleSuccess(ctx, nil)
}

func (h *UdpForWardingHandler) DeleteUdpForWarding(ctx *gin.Context) {
	req := new(v1.DeleteUdpForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.udpForWardingService.DeleteUdpForwarding(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 *UdpForWardingHandler) GetUdpForWardingList(ctx *gin.Context) {
	req := new(v1.GetForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.udpForWardingService.GetUdpForwardingWafUdpAllIps(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}