package bootstrap

import (
	"github.com/break/junhong_cmp_fiber/internal/handler/admin"
	"github.com/break/junhong_cmp_fiber/internal/handler/app"
	authHandler "github.com/break/junhong_cmp_fiber/internal/handler/auth"
	"github.com/break/junhong_cmp_fiber/internal/handler/callback"
	pollingPkg "github.com/break/junhong_cmp_fiber/internal/polling"
	clientOrderSvc "github.com/break/junhong_cmp_fiber/internal/service/client_order"
	pollingSvcPkg "github.com/break/junhong_cmp_fiber/internal/service/polling"
	"github.com/break/junhong_cmp_fiber/internal/store/postgres"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/go-playground/validator/v10"
)

func initHandlers(svc *services, deps *Dependencies) *Handlers {
	validate := validator.New()
	personalCustomerDeviceStore := postgres.NewPersonalCustomerDeviceStore(deps.DB)
	assetWalletStore := postgres.NewAssetWalletStore(deps.DB, deps.Redis)
	packageStore := postgres.NewPackageStore(deps.DB)
	shopPackageAllocationStore := postgres.NewShopPackageAllocationStore(deps.DB)
	iotCardStore := postgres.NewIotCardStore(deps.DB, deps.Redis)
	deviceStore := postgres.NewDeviceStore(deps.DB, deps.Redis)
	assetWalletTransactionStore := postgres.NewAssetWalletTransactionStore(deps.DB, deps.Redis)
	assetRechargeStore := postgres.NewAssetRechargeStore(deps.DB, deps.Redis)
	personalCustomerOpenIDStore := postgres.NewPersonalCustomerOpenIDStore(deps.DB)
	personalCustomerStore := postgres.NewPersonalCustomerStore(deps.DB, deps.Redis)
	personalCustomerPhoneStore := postgres.NewPersonalCustomerPhoneStore(deps.DB)
	orderStore := postgres.NewOrderStore(deps.DB, deps.Redis)
	packageSeriesStore := postgres.NewPackageSeriesStore(deps.DB)
	shopSeriesAllocationStore := postgres.NewShopSeriesAllocationStore(deps.DB)
	deviceSimBindingStore := postgres.NewDeviceSimBindingStore(deps.DB, deps.Redis)
	carrierStore := postgres.NewCarrierStore(deps.DB)
	clientOrderService := clientOrderSvc.New(
		svc.Asset,
		svc.PurchaseValidation,
		orderStore,
		assetRechargeStore,
		assetWalletStore,
		personalCustomerDeviceStore,
		personalCustomerOpenIDStore,
		personalCustomerStore,
		personalCustomerPhoneStore,
		svc.WechatConfig,
		svc.Order,
		packageSeriesStore,
		shopSeriesAllocationStore,
		iotCardStore,
		deviceStore,
		deps.DB,
		deps.Redis,
		deps.Logger,
	)

	return &Handlers{
		Auth:                        authHandler.NewHandler(svc.Auth, validate),
		Account:                     admin.NewAccountHandler(svc.Account),
		Role:                        admin.NewRoleHandler(svc.Role, validate),
		Permission:                  admin.NewPermissionHandler(svc.Permission),
		PersonalCustomer:            app.NewPersonalCustomerHandler(svc.PersonalCustomer, deps.Logger),
		ClientAuth:                  app.NewClientAuthHandler(svc.ClientAuth, deps.Logger),
		ClientAsset:                 app.NewClientAssetHandler(svc.Asset, personalCustomerDeviceStore, assetWalletStore, packageStore, shopPackageAllocationStore, iotCardStore, deviceStore, deps.DB, deps.Logger),
		ClientWallet:                app.NewClientWalletHandler(svc.Asset, personalCustomerDeviceStore, assetWalletStore, assetWalletTransactionStore, assetRechargeStore, svc.Recharge, personalCustomerOpenIDStore, svc.WechatConfig, deps.Redis, deps.Logger, deps.DB, iotCardStore, deviceStore),
		ClientOrder:                 app.NewClientOrderHandler(clientOrderService, deps.Logger),
		ClientExchange:              app.NewClientExchangeHandler(svc.Exchange),
		ClientRealname:              app.NewClientRealnameHandler(svc.Asset, personalCustomerDeviceStore, iotCardStore, deviceSimBindingStore, carrierStore, deps.GatewayClient, deps.Logger, svc.PollingManualTrigger),
		ClientDevice:                app.NewClientDeviceHandler(svc.Asset, personalCustomerDeviceStore, deviceStore, deviceSimBindingStore, iotCardStore, deps.GatewayClient, deps.Logger),
		Shop:                        admin.NewShopHandler(svc.Shop),
		ShopRole:                    admin.NewShopRoleHandler(svc.Shop),
		AdminAuth:                   admin.NewAuthHandler(svc.Auth, validate),
		ShopCommission:              admin.NewShopCommissionHandler(svc.ShopCommission),
		CommissionWithdrawal:        admin.NewCommissionWithdrawalHandler(svc.CommissionWithdrawal, validate),
		CommissionWithdrawalSetting: admin.NewCommissionWithdrawalSettingHandler(svc.CommissionWithdrawalSetting),
		Enterprise:                  admin.NewEnterpriseHandler(svc.Enterprise),
		EnterpriseCard:              admin.NewEnterpriseCardHandler(svc.EnterpriseCard),
		EnterpriseDevice:            admin.NewEnterpriseDeviceHandler(svc.EnterpriseDevice),
		Authorization:               admin.NewAuthorizationHandler(svc.Authorization),
		IotCard:                     admin.NewIotCardHandler(svc.IotCard),
		IotCardImport:               admin.NewIotCardImportHandler(svc.IotCardImport),
		Device:                      admin.NewDeviceHandler(svc.Device),
		DeviceImport:                admin.NewDeviceImportHandler(svc.DeviceImport),
		AssetAllocationRecord:       admin.NewAssetAllocationRecordHandler(svc.AssetAllocationRecord),
		Storage:                     admin.NewStorageHandler(deps.StorageService),
		Carrier:                     admin.NewCarrierHandler(svc.Carrier),
		PackageSeries:               admin.NewPackageSeriesHandler(svc.PackageSeries),
		Package:                     admin.NewPackageHandler(svc.Package),
		PackageUsage:                admin.NewPackageUsageHandler(svc.PackageDailyRecord),
		ShopPackageBatchAllocation:  admin.NewShopPackageBatchAllocationHandler(svc.ShopPackageBatchAllocation),
		ShopPackageBatchPricing:     admin.NewShopPackageBatchPricingHandler(svc.ShopPackageBatchPricing),
		ShopSeriesGrant:             admin.NewShopSeriesGrantHandler(svc.ShopSeriesGrant),
		AdminOrder:                  admin.NewOrderHandler(svc.Order, validate),
		AdminExchange:               admin.NewExchangeHandler(svc.Exchange, validate),
		PaymentCallback:             callback.NewPaymentHandler(svc.Order, svc.Recharge, svc.AgentRecharge, deps.WechatPayment, svc.WechatConfig, deps.Logger),
		PollingConfig:               admin.NewPollingConfigHandler(svc.PollingConfig),
		PollingConcurrency:          admin.NewPollingConcurrencyHandler(svc.PollingConcurrency),
		PollingMonitoring:           admin.NewPollingMonitoringHandler(svc.PollingMonitoring),
		PollingAlert:                admin.NewPollingAlertHandler(svc.PollingAlert),
		PollingCleanup:              admin.NewPollingCleanupHandler(svc.PollingCleanup),
		PollingManualTrigger:        admin.NewPollingManualTriggerHandler(svc.PollingManualTrigger),
		Asset: func() *admin.AssetHandler {
			pollingQueueMgr := pollingPkg.NewPollingQueueManager(deps.Redis, constants.PollingShardCount, deps.Logger)
			assetPollingSvc := pollingSvcPkg.NewAssetPollingService(deviceStore, deviceSimBindingStore, svc.IotCard, pollingQueueMgr, deps.Logger)
			h := admin.NewAssetHandler(svc.Asset, svc.Device, svc.StopResumeService, assetPollingSvc)
			h.SetLifecycleService(svc.AssetLifecycle)
			return h
		}(),
		AssetLifecycle: admin.NewAssetLifecycleHandler(svc.AssetLifecycle),
		AssetWallet: func() *admin.AssetWalletHandler {
			h := admin.NewAssetWalletHandler(svc.AssetWallet)
			h.SetAssetService(svc.Asset)
			return h
		}(),
		WechatConfig:  admin.NewWechatConfigHandler(svc.WechatConfig),
		AgentRecharge: admin.NewAgentRechargeHandler(svc.AgentRecharge, validate),
		Refund:        admin.NewRefundHandler(svc.Refund),
	}
}
