package initialization

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
	testdata "testData/api/test.data"
	_ "testData/docs"
	"testData/middleware"
	"testData/model/response"
	"testData/router"
)

func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Recovery(), middleware.Cors(), middleware.Log())
	// 判断是否线上环境
	if viper.GetBool("dev") {
		r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	}
	r.StaticFS("files", http.Dir("./files"))

	testData := r.Group("testData")
	{
		router.InitChartRouter(testData)
		router.InitReportListRouter(testData)
		router.InitReportChartRouter(testData)
	}
	testDataUpload := r.Group("testData")
	testDataUploadService := testdata.InitUpload()
	{
		testDataUpload.POST("upload", testDataUploadService.UploadAnswerFile)
	}
	r.NoRoute(func(c *gin.Context) {
		response.FailWithMessage("Not Found", c)
	})
	return r
}
