package bookmarks

import (
	"netidhub-saas-be/app/middleware"
	"netidhub-saas-be/app/module/bookmarks/controller"
	"netidhub-saas-be/app/module/bookmarks/repository"
	"netidhub-saas-be/app/module/bookmarks/service"
	usersRepo "netidhub-saas-be/app/module/users/repository"

	"github.com/gofiber/fiber/v2"
	"go.uber.org/fx"
)

// BookmarksRouter struct of BookmarksRouter
type BookmarksRouter struct {
	App        fiber.Router
	Controller controller.BookmarksController
	UsersRepo  usersRepo.UsersRepository
}

// NewBookmarksModule register bulky of Bookmarks module
var NewBookmarksModule = fx.Options(
	// register repository of Bookmarks module
	fx.Provide(repository.NewBookmarksRepository),

	// register service of Bookmarks module
	fx.Provide(service.NewBookmarksService),

	// register controller of Bookmarks module
	fx.Provide(controller.NewBookmarksController),

	// register router of Bookmarks module
	fx.Provide(NewBookmarksRouter),
)

// NewBookmarksRouter init BookmarksRouter
func NewBookmarksRouter(fiber *fiber.App, controller controller.BookmarksController, usersRepo usersRepo.UsersRepository) *BookmarksRouter {
	return &BookmarksRouter{
		App:        fiber,
		Controller: controller,
		UsersRepo:  usersRepo,
	}
}

// RegisterBookmarksRoutes register routes of Bookmarks module
func (_i *BookmarksRouter) RegisterBookmarksRoutes() {
	// define controllers
	bookmarksController := _i.Controller

	// define routes
	_i.App.Route("/bookmarks", func(router fiber.Router) {
		// Add user middleware to extract user level from JWT token
		router.Use(middleware.UserMiddleware(_i.UsersRepo))

		// Public routes (require authentication)
		router.Get("/", bookmarksController.All)
		router.Get("/:id", bookmarksController.Show)
		router.Post("/", bookmarksController.Save)
		router.Delete("/:id", bookmarksController.Delete)

		// User-specific routes
		router.Get("/user", bookmarksController.GetByUserId)
		router.Post("/toggle/:articleId", bookmarksController.ToggleBookmark)
		router.Get("/summary", bookmarksController.GetBookmarkSummary)
	})
}
