package controller

import (
	"context"
	"errors"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"gitlab.com/tensorsecurity-rd/waf-console/internal/service"
	"gitlab.com/tensorsecurity-rd/waf-console/internal/utils"
	"gorm.io/gorm"
)

type WafController struct {
	service service.Service
}

func NewWafController(clusterClientManager *utils.ClusterClientManager, db *gorm.DB, gatewayUrl string) *WafController {
	return &WafController{
		service: service.NewWafService(clusterClientManager, db, gatewayUrl),
	}
}

func (c *WafController) Waf(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	gatewayName := ctx.Param("gateway_name")
	regionCode := ctx.Param("region_code")
	namespace := ctx.Param("namespace")
	waf, err := c.service.GetWaf(ctx1, regionCode, namespace, gatewayName)
	if err != nil {
		// logging.Get().Err(err).Msgf("get waf")
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	resp := &utils.SingleRespData{
		Item: waf,
	}
	utils.AssembleResponse(ctx, resp, nil)
}

func (c *WafController) GetWafGatewayInfo(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	cookie := ctx.Request.Header.Get("Cookie")
	if cookie == "" {
		utils.AssembleResponse(ctx, nil, errors.New("cookie is required"))
		return
	}

	var req service.GetWafGatewayInfoReq
	if err := ctx.BindJSON(&req); err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}

	info, err := c.service.GetWafGatewayInfo(ctx1, &req)
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, info, nil)
}

func (c *WafController) CreateWaf(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	var req []service.CreateWafReq
	if err := ctx.BindJSON(&req); err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	for _, r := range req {
		_, err := c.service.CreateWaf(ctx1, &r)
		if err != nil {
			utils.AssembleResponse(ctx, nil, err)
			return
		}
	}
	utils.AssembleResponse(ctx, nil, nil)
}

// `
// curl -X POST http://127.0.0.1:8080/v1/api/waf \
//      -H "Content-Type: application/json" \
//      -d '[
//            {
//              "region_code": "cn-east-1",
//              "gatewayname": "istio-ingressgateway",
// 			 "namespace": "istio-system",
// 			 "port": 80,
// 			 "host": ["*"]
//            }
//          ]'
// `

func (c *WafController) UpdateMode(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	var req service.UpdateModeReq
	if err := ctx.BindJSON(&req); err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}

	_, err := c.service.UpdateMode(ctx1, &req)
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}

func (c *WafController) UpdateRule(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	var req service.RuleRequest
	if err := ctx.BindJSON(&req); err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}

	err := c.service.UpdateRule(ctx1, &req)
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}

func (c *WafController) SaveRuleCategoryToDB(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	err := c.service.SaveRuleCategoryToDB(ctx1)
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}

func (c *WafController) EnableListenerWaf(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	var req service.EnableListenerWafReq
	if err := ctx.BindJSON(&req); err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}

	err := c.service.EnableListenerWaf(ctx1, &req)
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}

func (c *WafController) EnableGatewayWaf(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	var req service.EnableGatewayWafReq
	if err := ctx.BindJSON(&req); err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	req.Cookie = ctx.Request.Header.Get("Cookie")

	err := c.service.EnableGatewayWaf(ctx1, &req)
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}

func (c *WafController) EnableListenerWafs(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	var req service.EnableListenerWafsReq
	if err := ctx.BindJSON(&req); err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}

	err := c.service.EnableListenerWafs(ctx1, &req)
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}

func (c *WafController) DeleteListenerWaf(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	regionCode := ctx.Param("region_code")
	namespace := ctx.Param("namespace")
	gatewayName := ctx.Param("gateway_name")
	port, err := strconv.Atoi(ctx.Param("port"))
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}

	err = c.service.DeleteListenerWaf(ctx1, &service.DeleteListenerReq{
		GatewateInfo: service.GatewateInfo{
			RegionCode:  regionCode,
			Namespace:   namespace,
			GatewayName: gatewayName,
		},
		Port: port,
	})
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}

func (c *WafController) DeleteGatewayWaf(ctx *gin.Context) {
	ctx1, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	regionCode := ctx.Param("region_code")
	namespace := ctx.Param("namespace")
	gatewayName := ctx.Param("gateway_name")

	err := c.service.DeleteGatewayWaf(ctx1, &service.GatewateInfo{
		RegionCode:  regionCode,
		Namespace:   namespace,
		GatewayName: gatewayName,
	})
	if err != nil {
		utils.AssembleResponse(ctx, nil, err)
		return
	}
	utils.AssembleResponse(ctx, nil, nil)
}
