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 InitQuizRouter(db *gorm.DB, router *gin.Engine) {
	r := NewQuizRouter(db, router)
	r.SetQuizRouter(db, router)
}

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

type quizRouter struct {
	db         *gorm.DB
	repository repositories.QuizRepository
	service    services.QuizService
	controller controllers.QuizController
	router     *gin.Engine
}

func NewQuizRouter(db *gorm.DB, router *gin.Engine) *quizRouter {
	repository := repositories.NewQuizRepository(db)
	service := services.NewQuizService(repository)
	controller := controllers.NewQuizController(service)

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

func (r *quizRouter) SetQuizRouter(db *gorm.DB, router *gin.Engine) {
	group := router.Group("/quiz")
	group.GET("", middleware.Auth(), r.controller.List)
	group.GET("/:id", middleware.Auth(), r.controller.Find)
	group.POST("", middleware.Auth(), r.controller.Create)
	group.PUT("/:id", middleware.Auth(), r.controller.Update)
}
