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 AllowAndDenyIpHandler struct {
	*handler.Handler
	allowAndDenyIpService waf.AllowAndDenyIpService
	wafLogService         admin.WafLogService
}

func NewAllowAndDenyIpHandler(
    handler *handler.Handler,
    allowAndDenyIpService waf.AllowAndDenyIpService,
	wafLogService admin.WafLogService,
) *AllowAndDenyIpHandler {
	return &AllowAndDenyIpHandler{
		Handler:      handler,
		allowAndDenyIpService: allowAndDenyIpService,
		wafLogService : wafLogService,
	}
}

func (h *AllowAndDenyIpHandler) GetAllowAndDenyIp(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.allowAndDenyIpService.GetAllowAndDenyIp(ctx, int64(req.Id))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *AllowAndDenyIpHandler) GetAllowAndDenyIpList(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.allowAndDenyIpService.GetAllowAndDenyIpsAllByHostId(ctx, int64(req.HostId))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *AllowAndDenyIpHandler) AddAllowAndDenyIp(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	id,err := h.allowAndDenyIpService.AddAllowAndDenyIps(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 *AllowAndDenyIpHandler) EditAllowAndDenyIp(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.allowAndDenyIpService.EditAllowAndDenyIps(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.Id,
		ExtraData:  req,
	})
	v1.HandleSuccess(ctx, nil)
}

func (h *AllowAndDenyIpHandler) DeleteAllowAndDenyIp(ctx *gin.Context) {
	var req v1.DelAllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.allowAndDenyIpService.DeleteAllowAndDenyIps(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)
}