package routers

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

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

func InitAuthRouter(db *gorm.DB, router *gin.Engine) {
	r := NewAuthRouter(db, router)
	r.SetAuthRouter(db, router)
}

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

type authRouter struct {
	db              *gorm.DB
	userRepository  repositories.UserRepository
	tokenRepository repositories.TokenRepository
	service         services.AuthService
	tokenService    services.TokenService
	controller      controllers.AuthController
	router          *gin.Engine
}

func NewAuthRouter(db *gorm.DB, router *gin.Engine) *authRouter {
	userRepository := repositories.NewUserRepository(db)
	tokenRepository := repositories.NewTokenRepository(db)
	service := services.NewAuthService(userRepository, tokenRepository)
	tokenService := services.NewTokenService(tokenRepository)
	controller := controllers.NewAuthController(service, tokenService)

	return &authRouter{
		db:              db,
		userRepository:  userRepository,
		tokenRepository: tokenRepository,
		service:         service,
		tokenService:    tokenService,
		controller:      controller,
		router:          router,
	}
}

func (r *authRouter) SetAuthRouter(db *gorm.DB, router *gin.Engine) {
	group := router.Group("/auth")
	group.POST("login", r.controller.Login)
	group.POST("register", r.controller.Register)
}
