package itcronjob

import (
	"github.com/gin-gonic/gin"

	health "data-platform/internal/dataplatform/controller/health-check"
	reto "data-platform/internal/dataplatform/controller/retool"
	"data-platform/internal/dataplatform/store/mysql"
	"data-platform/internal/pkg/code"
	"data-platform/internal/pkg/middleware"
	"data-platform/pkg/errors"
	"data-platform/pkg/util/ginres"
)

func initRouter(g *gin.Engine) {
	installController(g)
}

func installController(g *gin.Engine) *gin.Engine {
	g.NoRoute(func(c *gin.Context) {
		// with url
		ginres.WriteResponse(c, errors.WithCode(code.ErrNotFound, "Not found."+c.Request.URL.Path), nil)
	})

	store, _ := mysql.GetMySQLFactoryOr(nil)
	authRoute := g.Group("", middleware.Auth())

	internalGroup := authRoute.Group("/internal")
	{
		healCtrl := health.NewHealCheckController(store)
		internalGroup.GET("/health", healCtrl.HealCheck)
	}
	retool := authRoute.Group("/v1")
	{
		retoCtrl := reto.NewRetoController(store)
		retool.POST("/sf_single", retoCtrl.SalesforceSingle)
		retool.POST("/sf_single_sync", retoCtrl.SalesforceSingleSync)
		retool.POST("/sf_invoice_month", retoCtrl.SalesforceLastInvoiceMonth)
		retool.POST("/ar_table", retoCtrl.ARTable)
		retool.POST("/cloud_breakdown", retoCtrl.GenerateCloudBreakdown)
		retool.POST("/download", retoCtrl.Download)
		retool.POST("/upload", retoCtrl.Upload)
		retool.POST("/event_center", retoCtrl.EventCenter)
		retool.POST("/fix", retoCtrl.Fix)
	}

	return g
}
