package router

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

	"mengyaping-backend/handlers"
)

// SetupRouter 设置路由
func SetupRouter() *gin.Engine {
	r := gin.Default()

	// CORS配置
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Accept", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
	}))

	// 创建处理器
	websiteHandler := handlers.NewWebsiteHandler()

	// API路由组
	api := r.Group("/api")
	{
		// 健康检查
		api.GET("/health", func(c *gin.Context) {
			c.JSON(200, gin.H{
				"status":  "ok",
				"message": "服务运行正常",
			})
		})

		// 网站相关
		api.GET("/websites", websiteHandler.GetWebsites)
		api.GET("/websites/:id", websiteHandler.GetWebsite)
		api.POST("/websites", websiteHandler.CreateWebsite)
		api.PUT("/websites/:id", websiteHandler.UpdateWebsite)
		api.DELETE("/websites/:id", websiteHandler.DeleteWebsite)
		api.POST("/websites/:id/check", websiteHandler.CheckWebsiteNow)

		// 分组相关
		api.GET("/groups", websiteHandler.GetGroups)
		api.POST("/groups", websiteHandler.AddGroup)
	}

	return r
}
