package schedules

import (
	"web-qudo-be/app/middleware"
	"web-qudo-be/app/module/schedules/controller"
	"web-qudo-be/app/module/schedules/repository"
	"web-qudo-be/app/module/schedules/service"
	usersRepo "web-qudo-be/app/module/users/repository"

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

// SchedulesRouter struct of SchedulesRouter
type SchedulesRouter struct {
	App        fiber.Router
	Controller controller.SchedulesController
	UsersRepo  usersRepo.UsersRepository
}

// NewSchedulesModule register bulky of Schedules module
var NewSchedulesModule = fx.Options(
	// register repository of Schedules module
	fx.Provide(repository.NewSchedulesRepository),

	// register service of Schedules module
	fx.Provide(service.NewSchedulesService),

	// register controller of Schedules module
	fx.Provide(controller.NewSchedulesController),

	// register router of Schedules module
	fx.Provide(NewSchedulesRouter),
)

// NewSchedulesRouter init SchedulesRouter
func NewSchedulesRouter(fiber *fiber.App, controller controller.SchedulesController, usersRepo usersRepo.UsersRepository) *SchedulesRouter {
	return &SchedulesRouter{
		App:        fiber,
		Controller: controller,
		UsersRepo:  usersRepo,
	}
}

// RegisterSchedulesRoutes register routes of Schedules module
func (_i *SchedulesRouter) RegisterSchedulesRoutes() {
	// define controllers
	schedulesController := _i.Controller

	// define routes
	_i.App.Route("/schedules", func(router fiber.Router) {
		// Add user middleware to extract user level from JWT token
		router.Use(middleware.UserMiddleware(_i.UsersRepo))
		router.Get("/", schedulesController.All)
		router.Get("/:id", schedulesController.Show)
		router.Post("/", schedulesController.Save)
		router.Put("/:id", schedulesController.Update)
		router.Delete("/:id", schedulesController.Delete)
	})
}
