package routers

import (
	"learnsteam/learsteam-quiz-api/internal/controllers"
	"learnsteam/learsteam-quiz-api/internal/middleware"
	"learnsteam/learsteam-quiz-api/internal/repositories"
	"learnsteam/learsteam-quiz-api/internal/services"

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

func InitUserRouter(db *gorm.DB, router *gin.Engine) {
	r := NewUserRouter(db, router)
	r.SetUserRouter(db, router)
}

type UserRouter interface {
	SetRouter(db *gorm.DB, router *gin.Engine)
}

type userRouter struct {
	db         *gorm.DB
	repository repositories.UserRepository
	service    services.UserService
	controller controllers.UserController
	router     *gin.Engine
}

func NewUserRouter(db *gorm.DB, router *gin.Engine) *userRouter {
	repository := repositories.NewUserRepository(db)
	tokenRepository := repositories.NewTokenRepository(db)
	service := services.NewUserService(repository, tokenRepository)

	tokenService := services.NewTokenService(tokenRepository)
	controller := controllers.NewUserController(service, tokenService)

	return &userRouter{
		db:         db,
		repository: repository,
		service:    service,
		controller: controller,
		router:     router,
	}
}

func (r *userRouter) SetUserRouter(db *gorm.DB, router *gin.Engine) {
	group := router.Group("/user")
	group.GET("", middleware.Auth(), r.controller.List)
	group.GET("/:id", middleware.Auth(), r.controller.Find)
}
