//go:build ignore

package main

import (
	"log"
	"net/http"
	"strings"
	"xboard-go/internal/config"
	"xboard-go/internal/database"
	"xboard-go/internal/handler"
	"xboard-go/internal/middleware"

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

func main() {
	// Initialize configuration
	config.LoadConfig()

	// Initialize database
	database.InitDB()

	// Initialize Gin router
	r := gin.Default()

	// Global Middleware
	r.Use(gin.Recovery())

	// API Groups
	v1 := r.Group("/api/v1")
	{
		// Passport (Auth)
		passport := v1.Group("/passport")
		{
			passport.POST("/login", handler.Login)
			passport.POST("/register", handler.Register)
		}

		// Subscription (Client) Routes
		v1.GET("/s/:token", handler.Subscribe)

		// Authenticated Routes
		auth := v1.Group("")
		auth.Use(middleware.Auth())
		{
			// User module
			user := auth.Group("/user")
			{
				user.GET("/info", handler.UserInfo)
			}
		}

		// Admin Routes
		admin := auth.Group("/admin")
		admin.Use(middleware.AdminAuth())
		{
			admin.GET("/stats", func(c *gin.Context) {
				c.JSON(200, gin.H{"message": "Admin Stats"})
			})
		}

		// Admin Portal (Secure Path Entry)
		v1.GET("/:path", handler.AdminPortal)
		v1.GET("/:path/realname", handler.RealNameIndex)

		// Plugin API (Secure Path)
		realname := v1.Group("/api/v1/:path/realname")
		realname.Use(middleware.AdminAuth()) // Ensure only admins can access plugin APIs
		{
			realname.GET("/records", handler.RealNameRecords)
			realname.POST("/review/:id", handler.RealNameReview)
		}

		// Node (UniProxy) Routes
		server := v1.Group("/server")
		server.Use(middleware.NodeAuth())
		{
			uniProxy := server.Group("/uniProxy")
			{
				uniProxy.GET("/user", handler.NodeUser)
				uniProxy.POST("/push", handler.NodePush)
			}
		}
	}

	// Start server
	log.Printf("Server starting on port %s", config.AppConfig.AppPort)
	if err := r.Run(":" + config.AppConfig.AppPort); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
}
