package router

import (
	"metazone.cc/metalab/internal/config"
	"metazone.cc/metalab/internal/middleware"

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

// setupAPIRoutes 注册 API 路由
func setupAPIRoutes(r *gin.Engine, cfg *config.Config, d *dependencies) {
	// --- 设置页 API（需登录） ---
	settingsAPI := r.Group("/api/settings")
	settingsAPI.Use(d.authMdw.Required())
	settingsAPI.Use(middleware.CSRF(cfg))
	{
		settingsAPI.PUT("/profile", d.settingsCtrl.UpdateProfile)
		settingsAPI.POST("/avatar", d.settingsCtrl.UploadAvatar)
		settingsAPI.PUT("/password", d.settingsCtrl.ChangePassword)
		settingsAPI.POST("/delete-account", d.settingsCtrl.DeleteAccount)
		settingsAPI.GET("/audit-status", d.settingsCtrl.AuditStatus)
		// 登录管理
		settingsAPI.GET("/sessions", d.settingsCtrl.ListSessions)
		settingsAPI.DELETE("/sessions/:sid", d.settingsCtrl.DestroySession)
		settingsAPI.POST("/sessions/destroy-others", d.settingsCtrl.DestroyOtherSessions)
		settingsAPI.PUT("/sessions/:sid/remark", d.settingsCtrl.UpdateSessionRemark)
	}

	// --- 消息中心 API（需登录） ---
	messagesAPI := r.Group("/api/messages")
	messagesAPI.Use(d.authMdw.Required())
	messagesAPI.Use(middleware.CSRF(cfg))
	{
		messagesAPI.GET("/unread", d.messageCtrl.UnreadCount)
		messagesAPI.POST("/:id/read", d.messageCtrl.MarkRead)
		messagesAPI.POST("/read-all", d.messageCtrl.MarkAllRead)
	}

	// --- 认证 API ---
	api := r.Group("/api")
	api.Use(middleware.CSRF(cfg))
	{
		api.POST("/auth/check-email", d.authCtrl.CheckEmail)
		api.POST("/auth/register", d.authCtrl.Register)
		api.POST("/auth/login", d.authCtrl.Login)
		api.POST("/auth/confirm-restore", d.authCtrl.ConfirmRestore)
		api.POST("/auth/logout", d.authCtrl.Logout)
		api.POST("/auth/refresh", d.authCtrl.RefreshToken)

		// 帖子公开 API（无需登录）
		api.GET("/posts", d.postCtrl.ListAPI)
		api.GET("/posts/:id", d.postCtrl.ShowAPI)
	}

	// --- 帖子 API（需登录） ---
	postsAPI := r.Group("/api/posts")
	postsAPI.Use(d.authMdw.Required())
	postsAPI.Use(middleware.CSRF(cfg))
	{
		postsAPI.POST("", d.postCtrl.Create)
		postsAPI.PUT("/:id", d.postCtrl.Update)
		postsAPI.DELETE("/:id", d.postCtrl.Delete)
		postsAPI.POST("/:id/submit", d.postCtrl.Submit)
		postsAPI.POST("/upload-image", d.postCtrl.UploadImage)
	}

	// --- 创作中心 API（需登录） ---
	studioAPI := r.Group("/api/studio")
	studioAPI.Use(d.authMdw.Required())
	studioAPI.Use(middleware.CSRF(cfg))
	{
		studioAPI.GET("/overview", d.studioCtrl.OverviewAPI)
		studioAPI.GET("/posts", d.studioCtrl.ListPostsAPI)
		studioAPI.POST("/posts", d.studioCtrl.Create)
		studioAPI.PUT("/posts/:id", d.studioCtrl.Update)
		studioAPI.DELETE("/posts/:id", d.studioCtrl.Delete)
		studioAPI.POST("/posts/:id/submit", d.studioCtrl.Submit)
	}
}
