package main

import (
	"context"
	"fmt"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	"github.com/pingcap/dbaas/billing2/api"
	"github.com/pingcap/dbaas/billing2/config"
	"github.com/pingcap/dbaas/billing2/db"
	"github.com/pingcap/dbaas/billing2/handler/cachehandler"
	"github.com/pingcap/dbaas/billing2/handler/gcphandler/auth"
	"github.com/pingcap/dbaas/billing2/handler/metricshandler"
	"github.com/pingcap/dbaas/billing2/handler/ratehandler"
	"github.com/pingcap/dbaas/billing2/handler/sqsmeasurehandler"
	"github.com/pingcap/dbaas/billing2/utils/ginutils"
	"github.com/pingcap/dbaas/billing2/utils/log"
	"github.com/pingcap/dbaas/billing2/utils/panicIf"
	"github.com/pingcap/dbaas/billing2/utils/sentry"
	"github.com/tidbcloud/tiinf/sdk/auditlog"
	"go.uber.org/zap"
	"time"
)

func main() {
	gin.DisableConsoleColor()

	panicIf.Error(config.Init())
	// init some config value for stress server
	config.Get().Set(config.BillingStressTest, true)

	_, err := db.InitializeStressDB()
	panicIf.Error(err)
	cachehandler.Init()
	ratehandler.Init()
	panicIf.Error(auth.InitWebIdentityEnv())
	panicIf.Error(sentry.SetupLoggerAndSentry())

	engine := gin.Default()
	metricshandler.RegisterMetricsRoute(engine)
	stressGroup := engine.Group("/stress/billing")
	ws := ginutils.MyRoute{Route: stressGroup}
	// register test route
	ws.HandleWithError("GET", "/test", func(c *gin.Context) (ginutils.HandlerFuncResult, error) {
		log.Info("receive request")
		return "test success", nil
	})
	// register stress route
	api.InitializeMeasureRouter2Stress(ws)

	pprof.Register(engine)
	panicIf.Error(auditlog.InitAuditLogSDK(context.Background(), time.Second))
	panicIf.Error(sqsmeasurehandler.SetDefaultMeasureSender())
	//panicIf.Error(sqsmeasurehandler.Get().RunSqsMeasure())
	//panicIf.Error(eventbushandler.Get().Run())
	//paymenthandler.SetPaymentHandler(&impl.PaymentHandlerImpl{})

	stressServerPort := 16688
	log.Info(fmt.Sprintf("start stress server on port: %d", stressServerPort))
	log.Fatal("start stress server failed", zap.Error(engine.Run(fmt.Sprintf(":%d", stressServerPort))))
}
