package router

import (
	"metazone.cc/metalab/internal/config"
	"metazone.cc/metalab/internal/controller"
	adminCtrl "metazone.cc/metalab/internal/controller/admin"
	"metazone.cc/metalab/internal/middleware"
	"metazone.cc/metalab/internal/repository"
	"metazone.cc/metalab/internal/service"

	"gorm.io/gorm"
)

func buildDeps(db *gorm.DB, cfg *config.Config, siteSettings *config.SiteSettings) *dependencies {
	userRepo, sessionMgr, authService, _, authCtrl, avatarSvc, authMdw, adminController := buildDepsCore(db, cfg, siteSettings)

	auditRepo := repository.NewAuditRepo(db)
	notificationRepo := repository.NewNotificationRepo(db)
	notificationService := service.NewNotificationService(notificationRepo)
	auditService := service.NewAuditService(auditRepo, userRepo, siteSettings, cfg.Audit, notificationService)
	auditController := adminCtrl.NewAuditController(auditService, userRepo)

	messageController := controller.NewMessageController(notificationService)

	settingsCtrl := controller.NewSettingsController(authService, avatarSvc, auditService, sessionMgr)
	siteSettingController := adminCtrl.NewSiteSettingController(
		service.NewSiteSettingService(siteSettings),
		&service.AuditDefaults{
			Enabled: cfg.Audit.Enabled, UsernameAudit: cfg.Audit.UsernameAudit,
			AvatarAudit: cfg.Audit.AvatarAudit, BioAudit: cfg.Audit.BioAudit,
		},
	)

	postRepo := repository.NewPostRepo(db)
	postService := service.NewPostService(postRepo, siteSettings, notificationService)
	shortcodeSvc := service.NewShortcodeService()
	postCtrl := controller.NewPostController(postService, shortcodeSvc)
	adminPostCtrl := adminCtrl.NewAdminPostController(postService)
	studioCtrl := controller.NewStudioController(postService)

	// 用户空间
	spaceService := service.NewSpaceService(userRepo, postRepo)
	spaceCtrl := controller.NewSpaceController(spaceService)

	maintenanceMdw := middleware.NewMaintenanceMiddleware(cfg, siteSettings, sessionMgr)

	return &dependencies{
		authCtrl: authCtrl, authMdw: authMdw, maintenanceMdw: maintenanceMdw,
		settingsCtrl: settingsCtrl,
		messageCtrl: messageController, postCtrl: postCtrl, spaceCtrl: spaceCtrl,
		studioCtrl: studioCtrl,
		adminPostCtrl: adminPostCtrl,
		adminCtrl: adminController, auditCtrl: auditController,
		siteSettingCtrl: siteSettingController, tokenCtrl: authCtrl,
	}
}
