package api

import (
	"github.com/gin-gonic/gin"
	"github.com/olivere/elastic/v7"
	"gitlab.com/tensorsecurity-rd/waf-console/internal/controller"
	"gitlab.com/tensorsecurity-rd/waf-console/internal/utils"
	"gorm.io/gorm"
)

func SetWafRouter(e *gin.Engine, clusterClientManager *utils.ClusterClientManager, db *gorm.DB, gatewayUrl string, elasticClient *elastic.Client, regionUrlMap map[string]string) {
	v1 := e.Group("v1/api/waf")

	wafController := controller.NewWafController(clusterClientManager, db, gatewayUrl, elasticClient)
	v1.GET("", wafController.Waf)
	v1.GET("list", wafController.ListWafs)
	v1.POST("/", wafController.CreateWaf)
	v1.PUT("mode", wafController.UpdateMode)
	v1.PUT("rules", wafController.UpdateRule)
	v1.PUT("listener/enable", wafController.EnableListenerWaf)
	v1.PUT("gateway/enable", wafController.EnableGatewayWaf)
	v1.POST("listeners/enable", wafController.EnableListenerWafs)
	v1.DELETE("listener/:region_code/:namespace/:gateway_name/:port", wafController.DeleteListenerWaf)
	v1.DELETE("gateway/:region_code/:namespace/:gateway_name", wafController.DeleteGatewayWaf)
	v1.POST("debug/savecatagory", wafController.SaveRuleCategoryToDB)
	v1.GET("listener/history", wafController.ListListenerHistory)

	v2 := e.Group("api/v2/containerSec/waf")
	wafLogController := controller.NewWafLogController(regionUrlMap)
	v2.GET("attack/log/list", wafLogController.WafLogProxy)
	v2.GET("attack/log/details", wafLogController.WafLogProxy)
	v2.GET("attack/log/rspPkg", wafLogController.WafLogProxy)
	// v2.GET("attack/log/list", wafController.ListAttackLogs)
	// v2.GET("attack/log/details", wafController.GetAttackLogDetails)
	// v2.GET("attack/log/rspPkg", wafController.GetAttackLogRsp)

	v2.GET("rules", wafController.ListRules)
	v2.PUT("rules", wafController.UpdateRule)
	v2.POST("blackwhitelist", wafController.CreateBlackWhiteList)
	v2.PUT("blackwhitelist", wafController.UpdateBlackWhiteList)
	v2.PUT("blackwhitelist/enabling", wafController.EnableBlackWhiteList)
	v2.DELETE("blackwhitelist/:id", wafController.DeleteBlackWhiteList)
	v2.GET("blackwhitelists", wafController.GetBlackWhiteLists)
	v2.GET("services", wafController.ListWafsV2)
	v2.GET("attack/classes", wafController.AttackClassesList)

}

func SetApiWafRouter(e *gin.Engine, clusterClientManager *utils.ClusterClientManager, db *gorm.DB, gatewayUrl string, elasticClient *elastic.Client) {
	v1 := e.Group("api/v2/waf")

	wafController := controller.NewWafController(clusterClientManager, db, gatewayUrl, elasticClient)
	v1.GET("attack/log/list", wafController.ListAttackLogs)
	v1.GET("attack/log/details", wafController.GetAttackLogDetails)
	v1.GET("attack/log/rspPkg", wafController.GetAttackLogRsp)
}
