// handlers/handlers.go
package handlers

import (
	"api_bb/internal/config"
	"api_bb/internal/repository"
	"api_bb/internal/service"
	"api_bb/pkg/email"
	"api_bb/pkg/logger"

	"go.uber.org/zap"
	"gorm.io/gorm"
)

type Handler struct {
	healthHandler            *HealthHandler
	authHandler              *AuthHandler
	userHandler              *UserHandler
	avatarHandler            *AvatarHandler
	newsHandler              *NewsHandler
	reviewHandler            *ReviewHandler
	userStatsHandler         *UserStatsHandler
	userWorkoutHandler       *UserWorkoutHandler
	userAchievementHandler   *UserAchievementHandler
	eventHandler             *EventHandler
	eventRegistrationHandler *EventRegistrationHandler
	personalBestHandler      *PersonalBestHandler
	trainingPlanHandler      *TrainingPlanHandler
	emailHandler             *EmailHandler
	// Здесь будут добавлены другие обработчики
	// userHandler    *UserHandler
	// eventHandler   *EventHandler
	// reviewHandler  *ReviewHandler
}

func NewHandler(db *gorm.DB, cfg *config.Config) *Handler {
	// Инициализация репозиториев
	userRepo := repository.NewUserRepository(db)
	newsRepo := repository.NewNewsRepository(db)
	commentRepo := repository.NewCommentRepository(db)
	reviewRepo := repository.NewReviewRepository(db)
	userStatsRepo := repository.NewUserStatsRepository(db)
	userWorkoutRepo := repository.NewWorkoutRepository(db)
	userAchievemenRepo := repository.NewAchievementRepository(db)
	eventRepo := repository.NewEventRepository(db)
	eventRegistrationRepo := repository.NewEventRegistrationRepository(db)
	personalBestRepo := repository.NewPersonalBestRepository(db)
	trainingPlanRepo := repository.NewTrainingPlanRepository(db)
	emailRepo := repository.NewEmailRepository(db)

	// Initialize logger
	baseLogger := logger.NewWrapper(logger.Get()) // Создаем базовый логгер

	// getConfig
	emailSender, err := email.NewService(config.Load())
	if err != nil {
		baseLogger.Info("error to load config", zap.Error(err))
	}

	// Инициализация сервисов
	jwtService := service.NewJWTService(cfg.JWTSecret)
	authService := service.NewAuthService(userRepo, jwtService, baseLogger)
	userService := service.NewUserService(userRepo, jwtService, baseLogger)
	avatarService := service.NewAvatarService(userRepo, baseLogger)
	newsService := service.NewNewsService(newsRepo, commentRepo, baseLogger)
	reviewService := service.NewReviewService(reviewRepo, baseLogger)
	userStatsService := service.NewUserStatsService(userStatsRepo)
	userWorkoutService := service.NewWorkoutService(userWorkoutRepo)
	achievementService := service.NewAchievementService(userAchievemenRepo)
	eventRegistrationService := service.NewEventRegistrationService(eventRegistrationRepo, eventRepo, baseLogger)
	eventService := service.NewEventService(eventRepo, eventRegistrationRepo, baseLogger)
	personalBestService := service.NewPersonalBestService(personalBestRepo, userStatsService)
	trainingPlanService := service.NewTrainingPlanService(*trainingPlanRepo)
	emailService := service.NewEmailService(*emailRepo, userRepo, *emailSender)

	// Инициализация обработчиков
	healthHandler := NewHealthHandler()
	authHandler := NewAuthHandler(authService, jwtService, emailService)
	userHandler := NewUserHandler(&userService)
	newsHandler := NewNewsHandler(newsService, baseLogger)
	avatarHandler := NewAvatarHandler(avatarService)
	reviewHandler := NewReviewHandler(reviewService, baseLogger)
	userStatsHandler := NewUserStatsHandler(userStatsService)
	userWorkoutHandler := NewUserWorkoutHandler(userWorkoutService)
	userAchievementHandler := NewUserAchievementHandler(*achievementService)
	eventHandler := NewEventHandler(eventService)
	eventRegistrationHandler := NewEventRegistrationHandler(eventRegistrationService)
	personalBestHandler := NewPersonalBestHandler(*personalBestService)
	trainingPlanHandler := NewTrainingPlanHandler(trainingPlanService)
	emailHandler := NewEmailHandler(&emailService)

	return &Handler{
		healthHandler:            healthHandler,
		authHandler:              authHandler,
		userHandler:              userHandler,
		newsHandler:              newsHandler,
		avatarHandler:            avatarHandler,
		reviewHandler:            reviewHandler,
		userStatsHandler:         userStatsHandler,
		userWorkoutHandler:       userWorkoutHandler,
		userAchievementHandler:   userAchievementHandler,
		eventHandler:             eventHandler,
		eventRegistrationHandler: eventRegistrationHandler,
		personalBestHandler:      personalBestHandler,
		trainingPlanHandler:      trainingPlanHandler,
		emailHandler:             emailHandler,
	}
}

// Геттеры для обработчиков (опционально, для удобства)
func (h *Handler) EmailHandler() *EmailHandler {
	return h.emailHandler
}

func (h *Handler) TrainingPlanHandler() *TrainingPlanHandler {
	return h.trainingPlanHandler
}

func (h *Handler) PersonalBestHandler() *PersonalBestHandler {
	return h.personalBestHandler
}

func (h *Handler) EventHandler() *EventHandler {
	return h.eventHandler
}

func (h *Handler) EventRegistrationHandler() *EventRegistrationHandler {
	return h.eventRegistrationHandler
}

func (h *Handler) HealthHandler() *HealthHandler {
	return h.healthHandler
}

func (h *Handler) AuthHandler() *AuthHandler {
	return h.authHandler
}

func (h *Handler) UserHandler() *UserHandler {
	return h.userHandler
}

func (h *Handler) AvatarHandler() *AvatarHandler {
	return h.avatarHandler
}

func (h *Handler) NewsHandler() *NewsHandler {
	return h.newsHandler
}

func (h *Handler) ReviewHandler() *ReviewHandler {
	return h.reviewHandler
}

func (h *Handler) UserStatsHandler() *UserStatsHandler {
	return h.userStatsHandler
}

func (h *Handler) UserWorkoutHandler() *UserWorkoutHandler {
	return h.userWorkoutHandler
}

func (h *Handler) UserAchievementHandler() *UserAchievementHandler {
	return h.userAchievementHandler
}
