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"

	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
)

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

	auditRepo := repository.NewAuditRepo(db)
	notificationRepo := repository.NewNotificationRepo(db)
	notificationService := service.NewNotificationService(notificationRepo)

	// 域能系统
	energyRepo := repository.NewEnergyRepo(db)
	energyService := service.NewEnergyService(db, energyRepo, nil, notificationService)

	// 等级/签到/任务系统（链式注入域能服务）
	_, levelSvc, levelCtrl := buildDepsLevel(db, notificationService, authMdw)
	levelSvc.WithEnergyService(energyService)

	// EnergyService 也需要 LevelService（用于赋能获得经验），通过 setter 注入
	energyService.SetExpAdder(levelSvc)

	auditService := service.NewAuditService(auditRepo, userRepo, siteSettings, cfg.Audit, notificationService, levelSvc)
	auditService.WithEnergyRefundService(energyService)
	auditController := adminCtrl.NewAuditController(auditService, userRepo)

	messageController := controller.NewMessageController(notificationService)

	settingsCtrl := controller.NewSettingsController(authService, avatarSvc, auditService, sessionMgr, cfg, levelSvc)
	settingsCtrl.WithEnergyService(energyService)
	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)
	adminController := adminCtrl.NewAdminController(service.NewAdminService(userRepo, postRepo, sessionMgr))
	studioCtrl := controller.NewStudioController(postService)
	studioCtrl.WithEnergyService(energyService)

	// 域能控制器
	energyCtrl := controller.NewEnergyController(energyService)
	adminEnergyCtrl := adminCtrl.NewAdminEnergyController(energyService)

	// 用户空间
	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, postService: postService, spaceCtrl: spaceCtrl,
		studioCtrl:    studioCtrl,
		adminPostCtrl: adminPostCtrl,
		adminCtrl:     adminController, auditCtrl: auditController,
		siteSettingCtrl: siteSettingController,
		levelCtrl:       levelCtrl,
		energyCtrl:      energyCtrl,
		adminEnergyCtrl: adminEnergyCtrl,
		energyService:   energyService,
	}
}
