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 WebForwardingHandler struct {
	*handler.Handler
	webForwardingService waf.WebForwardingService
	wafLogService admin.WafLogService
}

func NewWebForwardingHandler(
	handler *handler.Handler,
	webForwardingService waf.WebForwardingService,
	wafLogService admin.WafLogService,
) *WebForwardingHandler {
	return &WebForwardingHandler{
		Handler:              handler,
		webForwardingService: webForwardingService,
		wafLogService : wafLogService,
	}
}

func (h *WebForwardingHandler) GetWebForwarding(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.webForwardingService.GetWebForwarding(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *WebForwardingHandler) AddWebForwarding(ctx *gin.Context) {
	req := new(v1.WebForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	id, err := h.webForwardingService.AddWebForwarding(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		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 *WebForwardingHandler) EditWebForwarding(ctx *gin.Context) {
	req := new(v1.WebForwardingRequest)

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

func (h *WebForwardingHandler) DeleteWebForwarding(ctx *gin.Context) {
	req := new(v1.DeleteWebForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.webForwardingService.DeleteWebForwarding(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 *WebForwardingHandler) GetWebForwardingList(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.webForwardingService.GetWebForwardingWafWebAllIps(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)

}