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 TcpforwardingHandler struct {
	*handler.Handler
	tcpforwardingService waf.TcpforwardingService
	wafLogService admin.WafLogService
}

func NewTcpforwardingHandler(
	handler *handler.Handler,
	tcpforwardingService waf.TcpforwardingService,
	wafLogService admin.WafLogService,
) *TcpforwardingHandler {
	return &TcpforwardingHandler{
		Handler:              handler,
		tcpforwardingService: tcpforwardingService,
		wafLogService:        wafLogService,
	}
}

func (h *TcpforwardingHandler) GetTcpforwarding(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.tcpforwardingService.GetTcpforwarding(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}

	v1.HandleSuccess(ctx, res)

}

func (h *TcpforwardingHandler) AddTcpForwarding(ctx *gin.Context) {
	req := new(v1.TcpForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	id, err := h.tcpforwardingService.AddTcpForwarding(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 *TcpforwardingHandler) EditTcpForwarding(ctx *gin.Context) {
	req := new(v1.TcpForwardingRequest)

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

func (h *TcpforwardingHandler) DeleteTcpForwarding(ctx *gin.Context) {
	req := new(v1.DeleteTcpForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.tcpforwardingService.DeleteTcpForwarding(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 *TcpforwardingHandler) GetTcpForwardingList(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.tcpforwardingService.GetTcpForwardingAllIpsByHostId(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}
