package v1

import (
	"api-service/internal/config"
	"api-service/internal/database"
	authHandlers "api-service/internal/handlers/auth"
	healthcheckHandlers "api-service/internal/handlers/healthcheck"
	qrisQrisHandlers "api-service/internal/handlers/qris"
	retribusiHandlers "api-service/internal/handlers/retribusi"
	"api-service/internal/middleware"
	services "api-service/internal/services/auth"
	"api-service/pkg/logger"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func RegisterRoutes(cfg *config.Config) *gin.Engine {
	router := gin.New()

	// Initialize auth middleware configuration
	middleware.InitializeAuth(cfg)

	// Add global middleware
	router.Use(middleware.CORSConfig())
	router.Use(middleware.ErrorHandler())
	router.Use(logger.RequestLoggerMiddleware(logger.Default()))
	router.Use(gin.Recovery())

	// Initialize services with error handling
	authService := services.NewAuthService(cfg)
	if authService == nil {
		logger.Fatal("Failed to initialize auth service")
	}

	// Initialize database service for health check
	dbService := database.New(cfg)

	// Health check endpoint
	healthCheckHandler := healthcheckHandlers.NewHealthCheckHandler(dbService)
	sistem := router.Group("/api/sistem")
	sistem.GET("/health", healthCheckHandler.CheckHealth)

	// Swagger UI route
	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// API v1 group
	v1 := router.Group("/api/v1")

	// =============================================================================
	// PUBLIC ROUTES (No Authentication Required)
	// =============================================================================

	// Authentication routes
	authHandler := authHandlers.NewAuthHandler(authService)
	tokenHandler := authHandlers.NewTokenHandler(authService)

	// Basic auth routes
	v1.POST("/auth/login", authHandler.Login)
	v1.POST("/auth/register", authHandler.Register)
	v1.POST("/auth/refresh", authHandler.RefreshToken)

	// Token generation routes (keep public if needed)
	v1.POST("/token/generate", tokenHandler.GenerateToken)
	v1.POST("/token/generate-direct", tokenHandler.GenerateTokenDirect)

	// ============= PUBLISHED ROUTES ===============================================

	// Qris endpoints
	qrisQrisHandler := qrisQrisHandlers.NewQrisHandler()
	qrisQrisGroup := v1.Group("/qris")
	{
		qrisQrisGroup.GET("", qrisQrisHandler.GetQris)
		qrisQrisGroup.GET("/:id", qrisQrisHandler.GetQrisByID)
		qrisQrisGroup.GET("/ip/:ip", qrisQrisHandler.GetQrisByIP)
		qrisQrisGroup.GET("/stats", qrisQrisHandler.GetQrisStats)
		qrisQrisGroup.GET("/dynamic", qrisQrisHandler.GetSearchDynamic)
		//qrisQrisGroup.GET("/search", qrisQrisHandler.SearchAdvanced)
	}

	// Search endpoints
	/*qrisSearchHandler := qrisSearchHandlers.NewSearchHandler()
	qrisSearchGroup := v1.Group("/qris/search")
	{
		//qrisSearchGroup.GET("", qrisSearchHandler.GetSearch)
		qrisSearchGroup.GET("/dynamic", qrisSearchHandler.GetSearchDynamic) // Route baru
		qrisSearchGroup.GET("/advanced", qrisSearchHandler.SearchAdvanced)  // Route pencarian
		//qrisSearchGroup.GET("/:id", qrisSearchHandler.GetSearchByID)
		qrisSearchGroup.GET("/stats", qrisSearchHandler.GetSearchStats)
	}*/

	// Retribusi endpoints
	retribusiHandler := retribusiHandlers.NewRetribusiHandler()
	retribusiGroup := v1.Group("/retribusi")
	{
		retribusiGroup.GET("", retribusiHandler.GetRetribusi)
		retribusiGroup.GET("/dynamic", retribusiHandler.GetRetribusiDynamic)    // Route baru
		retribusiGroup.GET("/search", retribusiHandler.SearchRetribusiAdvanced) // Route pencarian
		retribusiGroup.GET("/:id", retribusiHandler.GetRetribusiByID)
		retribusiGroup.POST("", retribusiHandler.CreateRetribusi)
		retribusiGroup.PUT("/:id", retribusiHandler.UpdateRetribusi)
		retribusiGroup.DELETE("/:id", retribusiHandler.DeleteRetribusi)
	}
	// =============================================================================
	// PROTECTED ROUTES (Authentication Required)
	// =============================================================================

	// Create protected group with configurable authentication
	protected := v1.Group("/")
	protected.Use(middleware.ConfigurableAuthMiddleware(cfg)) // Use configurable authentication

	// User profile (protected)
	protected.GET("/auth/me", authHandler.Me)

	// Retribusi endpoints (CRUD operations - should be protected)
	// retribusiHandler := retribusiHandlers.NewRetribusiHandler()
	// protectedRetribusi := protected.Group("/retribusi")
	// {
	// 	protectedRetribusi.GET("", retribusiHandler.GetRetribusi)           // GET /api/v1/retribusi
	// 	protectedRetribusi.GET("/:id", retribusiHandler.GetRetribusiByID)   // GET /api/v1/retribusi/:id
	// 	protectedRetribusi.POST("/", retribusiHandler.CreateRetribusi)      // POST /api/v1/retribusi/
	// 	protectedRetribusi.PUT("/:id", retribusiHandler.UpdateRetribusi)    // PUT /api/v1/retribusi/:id
	// 	protectedRetribusi.DELETE("/:id", retribusiHandler.DeleteRetribusi) // DELETE /api/v1/retribusi/:id
	// }

	// // BPJS VClaim endpoints (require authentication)
	// // Peserta routes
	// pesertaHandler := peserta.NewVClaimHandler(peserta.VClaimHandlerConfig{
	// 	BpjsConfig: cfg.Bpjs,
	// 	Logger:     *logger.Default(),
	// 	Validator:  nil,
	// })
	// protectedPeserta := protected.Group("/peserta")
	// protectedPeserta.GET("/peserta/:nokartu", pesertaHandler.GetPesertaBynokartu)
	// protectedPeserta.GET("/peserta/nik/:nik", pesertaHandler.GetPesertaBynik)

	// // Sep routes
	// sepHandler := sep.NewVClaimHandler(sep.VClaimHandlerConfig{
	// 	BpjsConfig: cfg.Bpjs,
	// 	Logger:     *logger.Default(),
	// 	Validator:  nil,
	// })
	// protectedSep := protected.Group("/sep")
	// protectedSep.GET("/sep/:nosep", sepHandler.GetSepSep)
	// protectedSep.POST("/sep", sepHandler.CreateSepSep)
	// protectedSep.PUT("/sep/:nosep", sepHandler.UpdateSepSep)
	// protectedSep.DELETE("/sep/:nosep", sepHandler.DeleteSepSep)

	return router
}
