package bootstrap

import (
	"github.com/break/junhong_cmp_fiber/internal/service/commission_calculation"
	"github.com/break/junhong_cmp_fiber/internal/service/commission_stats"
	iotCardSvc "github.com/break/junhong_cmp_fiber/internal/service/iot_card"
	orderSvc "github.com/break/junhong_cmp_fiber/internal/service/order"
	packagepkg "github.com/break/junhong_cmp_fiber/internal/service/package"
	pollingSvc "github.com/break/junhong_cmp_fiber/internal/service/polling"
	"github.com/break/junhong_cmp_fiber/pkg/queue"
)

type workerServices struct {
	CommissionCalculation *commission_calculation.Service
	CommissionStats       *commission_stats.Service
	UsageService          *packagepkg.UsageService
	ActivationService     *packagepkg.ActivationService
	ResetService          *packagepkg.ResetService
	AlertService          *pollingSvc.AlertService
	CleanupService        *pollingSvc.CleanupService
}

func initWorkerServices(stores *queue.WorkerStores, deps *WorkerDependencies) *queue.WorkerServices {
	commissionStatsService := commission_stats.New(stores.ShopSeriesCommissionStats)

	commissionCalculationService := commission_calculation.New(
		deps.DB,
		stores.CommissionRecord,
		stores.Shop,
		stores.ShopPackageAllocation,
		stores.ShopSeriesAllocation,
		stores.PackageSeries,
		stores.IotCard,
		stores.Device,
		stores.AgentWallet,
		stores.AgentWalletTransaction,
		stores.Order,
		stores.OrderItem,
		stores.Package,
		stores.ShopSeriesCommissionStats,
		commissionStatsService,
		deps.Logger,
	)

	usageService := packagepkg.NewUsageService(
		deps.DB,
		deps.Redis,
		stores.PackageUsage,
		stores.PackageUsageDailyRecord,
		stores.DeviceSimBinding,
		deps.Logger,
	)

	activationService := packagepkg.NewActivationService(
		deps.DB,
		deps.Redis,
		stores.PackageUsage,
		stores.Package,
		stores.PackageUsageDailyRecord,
		deps.Logger,
	)

	resetService := packagepkg.NewResetService(
		deps.DB,
		deps.Redis,
		stores.PackageUsage,
		deps.Logger,
	)

	alertService := pollingSvc.NewAlertService(
		stores.PollingAlertRule,
		stores.PollingAlertHistory,
		deps.Redis,
		deps.Logger,
	)

	cleanupService := pollingSvc.NewCleanupService(
		stores.DataCleanupConfig,
		stores.DataCleanupLog,
		deps.Logger,
	)

	// 初始化订单服务（仅用于超时自动取消，不需要微信支付和队列客户端）
	orderService := orderSvc.New(
		deps.DB,
		deps.Redis,
		stores.Order,
		stores.OrderItem,
		stores.AgentWallet,
		stores.AssetWallet,
		nil, // purchaseValidationService: 超时取消不需要
		stores.ShopPackageAllocation,
		stores.ShopSeriesAllocation,
		stores.IotCard,
		stores.Device,
		stores.PackageSeries,
		stores.PackageUsage,
		stores.Package,
		nil, // wechatConfigService: 超时取消不需要
		nil, // wechatPayment: 超时取消不需要
		nil, // paymentLoader: 超时取消不需要
		nil, // queueClient: 超时取消不触发分佣
		deps.Logger,
		stores.AssetIdentifier,
		stores.PersonalCustomer,
		stores.PersonalCustomerPhone,
	)

	// 创建停复机服务并注入回调：流量耗尽自动停机、套餐激活/重置/支付后自动复机
	stopResumeService := iotCardSvc.NewStopResumeService(deps.Redis, stores.IotCard, stores.PackageUsage, stores.DeviceSimBinding, deps.GatewayClient, deps.Logger)
	usageService.SetStopResumeCallback(stopResumeService)
	activationService.SetResumeCallback(stopResumeService)
	orderService.SetResumeCallback(stopResumeService)
	resetService.SetResumeCallback(stopResumeService)

	return &queue.WorkerServices{
		CommissionCalculation: commissionCalculationService,
		CommissionStats:       commissionStatsService,
		UsageService:          usageService,
		ActivationService:     activationService,
		ResetService:          resetService,
		AlertService:          alertService,
		CleanupService:        cleanupService,
		StopResumeService:     stopResumeService,
		OrderExpirer:          orderService,
	}
}
