package articles

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

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

// ArticlesRouter struct of ArticlesRouter
type ArticlesRouter struct {
	App        fiber.Router
	Controller *controller.Controller
	UsersRepo  usersRepo.UsersRepository
}

// NewArticlesModule register bulky of Articles module
var NewArticlesModule = fx.Options(
	// register repository of Articles module
	fx.Provide(repository.NewArticlesRepository),

	// register service of Articles module
	fx.Provide(service.NewArticlesService),

	// register controller of Articles module
	fx.Provide(controller.NewController),

	// register router of Articles module
	fx.Provide(NewArticlesRouter),
)

// NewArticlesRouter init ArticlesRouter
func NewArticlesRouter(fiber *fiber.App, controller *controller.Controller, usersRepo usersRepo.UsersRepository, articleApprovalFlowsSvc articleApprovalFlowsService.ArticleApprovalFlowsService) *ArticlesRouter {
	return &ArticlesRouter{
		App:        fiber,
		Controller: controller,
		UsersRepo:  usersRepo,
	}
}

// RegisterArticlesRoutes register routes of Articles module
func (_i *ArticlesRouter) RegisterArticlesRoutes() {
	// define controllers
	articlesController := _i.Controller.Articles

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

		// Dynamic approval system routes
		router.Post("/:id/submit-approval", articlesController.SubmitForApproval)
		router.Get("/:id/approval-status", articlesController.GetApprovalStatus)
		router.Get("/pending-approval", articlesController.GetPendingApprovals)
		router.Get("/waiting-for-approval", articlesController.GetArticlesWaitingForApproval)

		router.Get("/", articlesController.All)
		router.Get("/old-id/:id", articlesController.ShowByOldId)
		router.Get("/:id", articlesController.Show)
		router.Post("/", articlesController.Save)
		router.Put("/:id", articlesController.Update)
		router.Put("/banner/:id", articlesController.UpdateBanner)
		router.Post("/thumbnail/:id", articlesController.SaveThumbnail)
		router.Get("/thumbnail/viewer/:thumbnailName", articlesController.Viewer)
		router.Post("/publish-scheduling", articlesController.PublishScheduling)
		router.Delete("/:id", articlesController.Delete)
		router.Get("/statistic/summary", articlesController.SummaryStats)
		router.Get("/statistic/user-levels", articlesController.ArticlePerUserLevelStats)
		router.Get("/statistic/monthly", articlesController.ArticleMonthlyStats)
	})
}
