package api

import (
	"licserver/internal/middleware"
	"licserver/internal/utils"

	"github.com/gin-gonic/gin"
)

func SetupRouter(
	userHandler *UserHandler,
	deviceHandler *DeviceHandler,
	monitorHandler *MonitorHandler,
	config *utils.Config,
	uploadHandler *UploadHandler,
	siteHandler *SiteHandler,
	tokenHandler *TokenHandler,
	licenseHandler *LicenseHandler,
) *gin.Engine {
	r := gin.Default()

	// 添加错误处理中间件
	r.Use(middleware.ErrorHandler())

	// 静态文件服务
	r.Static("/static", "./web/static")

	// 首页和登录页面
	r.StaticFile("/", "./web/templates/index.html")
	r.StaticFile("/login", "./web/templates/login.html")

	// Admin页面路由组
	admin := r.Group("/admin")
	admin.Use(middleware.JWTAuth(&config.JWT))
	{
		// 使用StaticFile处理包含Layui模板的页面
		admin.StaticFile("/dashboard", "./web/templates/admin/dashboard.html")
		admin.StaticFile("/devices", "./web/templates/admin/devices.html")
		admin.StaticFile("/device-files", "./web/templates/admin/device-files.html")
		admin.StaticFile("/device-license", "./web/templates/admin/device-license.html")
		admin.StaticFile("/licenses", "./web/templates/admin/licenses.html")
		admin.StaticFile("/license-logs", "./web/templates/admin/license-logs.html")
		admin.StaticFile("/tokens", "./web/templates/admin/tokens.html")
		admin.StaticFile("/token-logs", "./web/templates/admin/token-logs.html")
		admin.StaticFile("/monitor", "./web/templates/admin/monitor.html")
		admin.StaticFile("/site-settings", "./web/templates/admin/site-settings.html")
		admin.StaticFile("/users", "./web/templates/admin/users.html")
		admin.StaticFile("/user-edit", "./web/templates/admin/user-edit.html")
		admin.StaticFile("/change-password", "./web/templates/admin/change-password.html")
	}

	// API路由
	api := r.Group("/api")
	{
		// 公开API
		api.GET("/captcha", userHandler.GetCaptcha)
		api.POST("/captcha/verify", userHandler.VerifyCaptcha)
		api.POST("/login", userHandler.Login)
		api.POST("/register", userHandler.Register)
		api.POST("/reset-password", userHandler.ResetPassword)
		api.POST("/reset-password/confirm", userHandler.ResetPasswordWithToken)
		api.POST("/captcha/register", userHandler.SendRegisterCaptcha)
		api.POST("/captcha/reset-password", userHandler.SendResetPasswordCaptcha)
		api.POST("/validate-token", tokenHandler.ValidateToken)
		api.POST("/devices/register", deviceHandler.RegisterDevice)

		// 需要认证的API
		authorized := api.Group("")
		authorized.Use(middleware.JWTAuth(&config.JWT))
		{
			// 设备型号管理
			authorized.POST("/devices/models", middleware.AdminRequired(), deviceHandler.CreateDeviceModel)
			authorized.GET("/devices/models", deviceHandler.GetDeviceModels)
			authorized.PUT("/devices/models/:id", middleware.AdminRequired(), deviceHandler.UpdateDeviceModel)
			authorized.DELETE("/devices/models/:id", middleware.AdminRequired(), deviceHandler.DeleteDeviceModel)
			authorized.POST("/devices/models/batch", middleware.AdminRequired(), deviceHandler.BatchDeleteDeviceModels)

			// 设备管理
			authorized.POST("/devices/register", deviceHandler.RegisterDevice)
			authorized.GET("/devices/registered", deviceHandler.GetRegisteredDevices)
			authorized.POST("/devices/:uid/license", middleware.AdminRequired(), deviceHandler.BindLicense)
			authorized.DELETE("/devices/:uid/license", middleware.AdminRequired(), deviceHandler.UnbindLicense)
			authorized.GET("/devices/:uid/logs", deviceHandler.GetDeviceLogs)

			// 其他API路由...
			// 用户管理
			authorized.GET("/users", middleware.AdminRequired(), userHandler.GetUsers)
			authorized.POST("/users", middleware.AdminRequired(), userHandler.CreateUser)
			authorized.GET("/users/:id", middleware.AdminRequired(), userHandler.GetUserInfo)
			authorized.PUT("/users/:id", middleware.AdminRequired(), userHandler.UpdateUser)
			authorized.DELETE("/users/:id", middleware.AdminRequired(), userHandler.DeleteUser)
			authorized.GET("/users/profile", userHandler.GetProfile)
			authorized.PUT("/users/profile", userHandler.UpdateProfile)
			authorized.POST("/users/change-password", userHandler.ChangePassword)

			// 系统监控
			authorized.GET("/monitor/status", middleware.AdminRequired(), monitorHandler.GetSystemStatus)

			// 站点设置
			authorized.GET("/site/settings", middleware.AdminRequired(), siteHandler.GetSettings)
			authorized.PUT("/site/settings", middleware.AdminRequired(), siteHandler.UpdateSettings)

			// Token管理
			authorized.POST("/tokens", middleware.AdminRequired(), tokenHandler.CreateToken)
			authorized.GET("/tokens", tokenHandler.GetTokens)
			authorized.GET("/tokens/:id/logs", tokenHandler.GetTokenLogs)
			authorized.DELETE("/tokens/:token", middleware.AdminRequired(), tokenHandler.RevokeToken)

			// 授权码管理
			authorized.POST("/licenses", middleware.AdminRequired(), licenseHandler.CreateLicenses)
			authorized.GET("/licenses", licenseHandler.GetLicenses)
			authorized.GET("/licenses/:id/logs", licenseHandler.GetLicenseLogs)
			authorized.POST("/licenses/use", licenseHandler.UseLicense)

			// 仪表盘统计
			authorized.GET("/dashboard/stats", deviceHandler.GetDashboardStats)
		}
	}

	return r

}
