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

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

type examRouter struct {
	db                *gorm.DB
	repository        repositories.ExamRepository
	programRepository repositories.ProgramRepository
	userRepository    repositories.UserRepository

	service        services.ExamService
	programService services.ProgramService
	userService    services.UserService

	controller controllers.ExamController

	router *gin.Engine
}

func NewExamRouter(db *gorm.DB, router *gin.Engine) *examRouter {
	repository := repositories.NewExamRepository(db)
	programRepository := repositories.NewProgramRepository(db)
	tokenRepository := repositories.NewTokenRepository(db)
	userRepository := repositories.NewUserRepository(db)

	service := services.NewExamService(repository)
	programService := services.NewProgramService(programRepository)
	userService := services.NewUserService(userRepository, tokenRepository)

	controller := controllers.NewExamController(service, userService, programService)

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

func (r *examRouter) SetExamRouter(db *gorm.DB, router *gin.Engine) {
	group := router.Group("/exam")
	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)
	group.PATCH("/:id", middleware.Auth(), r.controller.Patch)
}
