package routes

import (
	"ems-backend/controllers"
	"ems-backend/middleware"

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

func SetupRoutes(r *gin.Engine) {
	// Public Routes
	r.POST("/api/v1/login", controllers.Login)
	r.POST("/api/v1/login/sso", controllers.SSOLogin)

	api := r.Group("/api/v1")
	api.Use(middleware.AuthRequired())
	{
		// Menu & Perms
		api.GET("/getRouters", controllers.GetRouters)
		api.GET("/system/menu/tree", controllers.GetMenuTree)
		api.GET("/system/role/:roleId/menus", controllers.GetRoleMenuIds)

		// AI Analysis
		ai := api.Group("/ai")
		{
			ai.GET("/config", controllers.GetAIConfig)
			ai.POST("/config", controllers.UpdateAIConfig)
			ai.POST("/config/test", controllers.TestAIConnection)
			ai.POST("/config/models", controllers.GetRemoteModels)
			ai.POST("/generate-report", controllers.GenerateAIReport)
			ai.GET("/reports", controllers.GetAIReports)
			ai.GET("/reports/:id", controllers.GetAIReportDetail)
		}

		// Integration Sources
		api.GET("/sources", controllers.GetSources)
		api.POST("/sources/import/preview", controllers.PreviewSourceImport)
		api.POST("/sources/import/execute", controllers.ImportSources)
		api.POST("/sources", controllers.CreateSource)
		api.PUT("/sources/:id", controllers.UpdateSource)
		api.DELETE("/sources/:id", controllers.DeleteSource)
		api.POST("/sources/test", controllers.TestSourceConnection) // 测试连接
		api.GET("/sources/:id/candidates", controllers.GetSourceCandidates) // 获取候选设备 (Legacy)
		api.GET("/sources/:id/devices", controllers.GetSourceDevices) // 获取HA设备列表
		api.GET("/sources/:id/devices/:deviceId/entities", controllers.GetSourceDeviceEntities) // 获取HA设备实体
		api.POST("/sources/:id/sync", controllers.SyncSource)       // 立即同步
		api.POST("/sources/:id/service", controllers.CallSourceService) // 调用服务

		// Devices
		api.GET("/devices", controllers.GetDevices)
		api.GET("/devices/:id", controllers.GetDevice)
		api.GET("/devices/:id/realtime", controllers.GetDeviceRealtime) // 获取时序库最新数据
		api.GET("/devices/history", controllers.GetDeviceHistory)
		api.DELETE("/devices/history", controllers.DeleteDeviceHistory)
		api.POST("/devices", controllers.CreateDevice)
		api.PUT("/devices/:id", controllers.UpdateDevice)
		api.DELETE("/devices/:id", controllers.DeleteDevice)

		// Locations
		api.GET("/locations", controllers.GetLocations)
		api.POST("/locations", controllers.CreateLocation)
		api.PUT("/locations/:id", controllers.UpdateLocation)
		api.DELETE("/locations/:id", controllers.DeleteLocation)

		// Cleaning Templates
		api.GET("/cleaning-templates", controllers.GetCleaningTemplates)
		api.POST("/cleaning-templates", controllers.CreateCleaningTemplate)
		api.PUT("/cleaning-templates/:id", controllers.UpdateCleaningTemplate)
		api.DELETE("/cleaning-templates/:id", controllers.DeleteCleaningTemplate)

		// Dashboard & Control
		// api.GET("/dashboard/stats", controllers.GetDashboardStats) // Deprecated
		dashboard := api.Group("/dashboard")
		{
			dashboard.GET("/stats/energy", controllers.GetTodayEnergy)
			dashboard.GET("/stats/power", controllers.GetRealtimePower)
			dashboard.GET("/stats/overview", controllers.GetDeviceStats)
			dashboard.GET("/stats/trend", controllers.GetEnergyTrend)
			dashboard.GET("/stats/power-trend", controllers.GetPowerTrend)
			dashboard.GET("/stats/activities", controllers.GetRecentActivities)
		}
		api.POST("/control/:id", controllers.ControlDevice)

		// Alarms
		api.GET("/alarms", controllers.GetAlarms)
		api.POST("/alarms", controllers.CreateAlarm)
		api.PUT("/alarms/:id/ack", controllers.AcknowledgeAlarm)

		// Alarm Rules
		api.GET("/alarm-rules", controllers.GetAlarmRules)
		api.POST("/alarm-rules", controllers.CreateAlarmRule)
		api.DELETE("/alarm-rules/batch", controllers.BatchDeleteAlarmRules) // Batch Delete
		api.PUT("/alarm-rules/:id", controllers.UpdateAlarmRule)
		api.DELETE("/alarm-rules/:id", controllers.DeleteAlarmRule)

		// Energy Analysis
		api.GET("/analysis/energy", controllers.GetEnergyAnalysis)

		// Inspections
		api.GET("/inspections", controllers.GetInspections)
		api.POST("/inspections", controllers.CreateInspection)

			// System & Settings
		sys := api.Group("/system")
		{
			// Roles
			sys.GET("/roles", controllers.GetRoles)
			sys.POST("/roles", controllers.CreateRole)
			sys.PUT("/roles/:id", controllers.UpdateRole)
			sys.DELETE("/roles/:id", controllers.DeleteRole)

			// Users
			sys.GET("/users", controllers.GetUsers)
			sys.POST("/users", controllers.CreateUser)
			sys.PUT("/users/:id", controllers.UpdateUser)
			sys.DELETE("/users/:id", controllers.DeleteUser)

			// User Profile (Self)
			sys.GET("/user/profile", controllers.GetProfile)
			sys.PUT("/user/profile/updatePwd", controllers.UpdateProfilePwd)

			// Configs
			sys.GET("/configs", controllers.GetAllConfigs)
			sys.GET("/config/:key", controllers.GetConfig)
			sys.POST("/config", controllers.UpdateConfig)
			
			// Debug Log Control
			sys.POST("/debug-log", controllers.SetDebugLog)
		}

		// Backup
		backup := api.Group("/backup")
		{
			bc := new(controllers.BackupController)
			backup.GET("/config", bc.GetConfig)
			backup.POST("/config", bc.SaveConfig)
			backup.POST("/exec", bc.ManualBackup)
			backup.GET("/logs", bc.GetLogs)
			backup.POST("/test", bc.TestConnection)
			backup.GET("/download/:id", bc.Download)
		}

		// Operations Logs
		ops := api.Group("/ops")
		{
			lc := controllers.NewLogController()
			ops.GET("/logs/files", lc.GetLogFiles)
			ops.GET("/logs/content", lc.GetLogContent)
		}
	}
}

