import { logger } from '../../utils/logger';
import { DomainRegistry } from './DomainRegistry';
import { startRuntime } from '../../runtime';

// Core Governance
import { AuthService } from '../../services/AuthService';
import { TurboGateway } from '../gateway/TurboGateway';
import { FeatureGovernanceService } from '../governance/FeatureGovernanceService';
import { QuotaGovernanceService } from '../governance/QuotaGovernanceService';


import { DBShardingService } from './DBShardingService';
import { DomainEventBus } from './DomainEventBus';
import { EnvValidatorService } from './EnvValidatorService';
import { EventBusOptimizationService } from './EventBusOptimizationService';
import { SnowflakeIDService } from './SnowflakeIDService';
import { TsoaDocGenerator } from './TsoaDocGenerator';
import { WarmupService } from './WarmupService';

// Governance
import { FeatureToggleService } from '../governance/FeatureToggleService';
import { S3QuotaManager } from '../governance/S3QuotaManager';

// Business Services
import { ActionAuditService } from '../../services/ActionAuditService';
import { AgentSwarmService } from '../../services/AgentSwarmService';
import { AutoCircuitBreakerService } from '../../services/AutoCircuitBreakerService';
import { AutoDiagnosticsService } from '../../services/AutoDiagnosticsService';
import { AutonomousSandboxService } from '../../services/AutonomousSandboxService';
import { BehavioralRiskService } from '../../services/BehavioralRiskService';
import { BullMQDeadLetterService } from '../../services/BullMQDeadLetterService';
import { BusinessModelEvolutionService } from '../../services/BusinessModelEvolutionService';
import { CashflowForecastService } from '../../services/CashflowForecastService';
import { CashflowPredictor } from '../../services/CashflowPredictor';
import { ChannelStatusService } from '../../services/ChannelStatusService';
import { ContainerQuotaService } from '../../services/ContainerQuotaService';
import { CostAttributionService } from '../../services/CostAttributionService';
import { CurrencyRiskService } from '../../services/CurrencyRiskService';
import { DataComplianceService } from '../../services/DataComplianceService';
import { DeadlockAdvisor } from '../../services/DeadlockAdvisor';
import { DisputeResolverService } from '../../services/DisputeResolverService';
import { DynamicPricingService } from '../../services/DynamicPricingService';
import { FinanceReconciliationService } from '../../services/FinanceReconciliationService';
import { FraudSharedService } from '../../services/FraudSharedService';
import { MediaAssetService } from '../../services/MediaAssetService';
import { OmniStockService } from '../../services/OmniStockService';
import { OrderProfitService } from '../../services/OrderProfitService';
import { PredictiveHealthService } from '../../services/PredictiveHealthService';
import { PricingAuditService } from '../../services/PricingAuditService';
import { ProductHealthService } from '../../services/ProductHealthService';
import { QuotaCircuitBreakerService } from '../../services/QuotaCircuitBreakerService';
import { RedTeamingService } from '../../services/RedTeamingService';
import { ReportService } from '../../services/ReportService';
import { RBACService } from '../../services/RBACService';
import { FeatureActivationService } from '../../services/FeatureActivationService';
import { MerchantSettlementService } from '../../services/MerchantSettlementService';
import { MerchantProductOrderService } from '../../services/MerchantProductOrderService';
import { BillingService } from '../../services/BillingService';
import { ReviewService } from '../../services/ReviewService';
import { SemanticLogService } from '../../services/SemanticLogService';
import { SovereignReputationV2Service } from '../../services/SovereignReputationV2Service';
import { SupplierService } from '../../services/SupplierService';
import { TaxComplianceService } from '../../services/TaxComplianceService';
import { TracingTopoService } from '../../services/TracingTopoService';
import { TrueROASService } from '../../services/TrueROASService';
import { TradeService } from '../../services/TradeService';
import { VendorCreditService } from '../../services/VendorCreditService';
import { WorkerProfilerService } from '../../services/WorkerProfilerService';

// Runtime & Security
import { CacheConsistencyService } from '../security/CacheConsistencyService';
import { CertsMonitorService } from '../security/CertsMonitorService';
import { ContainerSecurityService } from '../security/ContainerSecurityService';
import { DIDHandshakeService } from '../security/DIDHandshakeService';
import { PermissionAuditService } from '../security/PermissionAuditService';
import { PrivateAuditService } from '../security/PrivateAuditService';
import { SecurityScanService } from '../security/SecurityScanService';
import { MemoryWatchdogService } from '../telemetry/MemoryWatchdogService';
import { TransactionScopeService } from '../telemetry/TransactionScopeService';
import { PluginManager } from './PluginManager';
import { PriorityAsyncEngine } from './PriorityAsyncEngine';

// AGI Core
import { ChatBotService } from '../ai/ChatBotService';
import { DecisionExplainabilityEngine } from '../ai/DecisionExplainabilityEngine';
import { ImageRecognitionService } from '../ai/ImageRecognitionService';
import { NaturalLanguageProcessingService } from '../ai/NaturalLanguageProcessingService';
import { RecommendationService } from '../ai/RecommendationService';

// Network & Federated (SOV_NET)
import { FederatedNodeService } from '../network/FederatedNodeService';
import { P2PConnectionService } from '../network/P2PConnectionService';


import { DisputeAdvisorService } from '../../services/DisputeAdvisorService';

// AI-2 Customer Domains
import { CustomerService } from '../../domains/Customer/CustomerService';
import { SupportService } from '../../domains/Customer/SupportService';

// AI-3 Business Domains (Finance)
import { AdOptimizerService } from '../../domains/Finance/AdOptimizerService';
import { CACThresholdService } from '../../domains/Finance/CACThresholdService';
import { CommissionService } from '../../domains/Finance/CommissionService';
import { CommodityHedgingService } from '../../domains/Finance/CommodityHedgingService';
import { CreditMonitorService } from '../../domains/Finance/CreditMonitorService';
import { ExpenseService } from '../../domains/Finance/ExpenseService';
import { FinancialAuditService } from '../../domains/Finance/FinancialAuditService';
import { FinancialPlanner } from '../../domains/Finance/FinancialPlanner';
import { InventoryValueService } from '../../domains/Finance/InventoryValueService';
import { InvoicingService } from '../../domains/Finance/InvoicingService';
import { LegalTaxService } from '../../domains/Finance/LegalTaxService';
import { MarginProtector } from '../../domains/Finance/MarginProtector';
import { MaterialRiskService } from '../../domains/Finance/MaterialRiskService';
import { MultiAssetSettlementService } from '../../domains/Finance/MultiAssetSettlementService';
import { PaymentTermsService } from '../../domains/Finance/PaymentTermsService';
import { PayoutCycleService } from '../../domains/Finance/PayoutCycleService';
import { PayoutOptimizer } from '../../domains/Finance/PayoutOptimizer';
import { PoolSourcingService } from '../../domains/Finance/PoolSourcingService';
import { RebateService } from '../../domains/Finance/RebateService';
import { ReconciliationService } from '../../domains/Finance/ReconciliationService';
import { RefundAuditService } from '../../domains/Finance/RefundAuditService';
import { RefundRiskService } from '../../domains/Finance/RefundRiskService';
import { RemitPathService } from '../../domains/Finance/RemitPathService';
import { SKUEfficiencyService } from '../../domains/Finance/SKUEfficiencyService';
import { SovereignCreditPoolService } from '../../domains/Finance/SovereignCreditPoolService';
import { SovereignWealthFundService } from '../../domains/Finance/SovereignWealthFundService';
import { StockAuditService } from '../../domains/Finance/StockAuditService';
import { TaxFilingAdvisor } from '../../domains/Finance/TaxFilingAdvisor';
import { TaxPlanningService } from '../../domains/Finance/TaxPlanningService';
import { TenantHealthService } from '../../domains/Finance/TenantHealthService';
import { TurnoverWatchdog } from '../../domains/Finance/TurnoverWatchdog';
import { WarrantyService } from '../../domains/Finance/WarrantyService';

// AI-3 Business Domains (Logistics)
import { CongestionFailoverService } from '../../domains/Logistics/CongestionFailoverService';
import { FreightAuditService } from '../../domains/Logistics/FreightAuditService';
import { LastMileAIService } from '../../domains/Logistics/LastMileAIService';
import { LastMileOptimizerService } from '../../domains/Logistics/LastMileOptimizerService';
import { LogisticTelemetryService } from '../../domains/Logistics/LogisticTelemetryService';
import { RouteOptimizerService } from '../../domains/Logistics/RouteOptimizerService';
import { SupplierScoringService } from '../../domains/Logistics/SupplierScoringService';

// AI-3 Business Domains (Marketing)
import { KOLService } from '../../domains/Marketing/KOLService';
import { MarketingCalendarService } from '../../domains/Marketing/MarketingCalendarService';
import { SocialPulseService } from '../../domains/Marketing/SocialPulseService';

// AI-3 Business Domains (Trade)
import { ConsumerOrderService } from '../../domains/Trade/ConsumerOrderService';
import { GlobalFulfillmentService } from '../../domains/Trade/GlobalFulfillmentService';
import { InventoryForecastService } from '../../domains/Trade/InventoryForecastService';
import { SovereignCarbonService } from '../../domains/Trade/SovereignCarbonService';
import { SovereignCrisisService } from '../../domains/Trade/SovereignCrisisService';

// Other BIZ Services
import { BillingEngine } from '../../domains/Finance/BillingEngine';
import { BillingService as DomainBillingService } from '../../domains/Billing/BillingService';
import { SLAGovernanceService } from '../../domains/Billing/SLAGovernanceService';
import { CreativeService } from '../../domains/Creative/CreativeService';
import { TenantService } from '../../domains/Tenant/TenantService';
import { OnboardingService } from '../biz/OnboardingService';

// Core Services
import { AuditService } from '../../services/AuditService';
import { ExperimentService } from '../../services/ExperimentService';
import { ProductService } from '../../services/ProductService';
import { SKUMappingService } from '../../services/SKUMappingService';
import { UnifiedTaskService } from '../../services/UnifiedTaskService';
import { SelfHealingService } from '../../services/SelfHealingService';
import { SyncService } from '../../services/SyncService';
import { VaultService } from '../../services/VaultService';
import { WarehouseService } from '../../services/WarehouseService';
import { WebhookService } from '../../services/WebhookService';
import { LegacyTableInitializer } from './LegacyTableInitializer';

/**
 * [ARCH_LIGHT_02] 领域启动加载器 (Domain Bootstrap Loader)
 * @description 核心逻辑：负责所有领域模块的按优先级注册与加载。
 * 将庞大的 index.ts 初始化流收敛于此，支持“基础模式”下的 AGI 模块按需加载。
 */
export class DomainBootstrap {
  static async init() {
    try {
      logger.info('[DomainBootstrap] Registering domain modules...');
      
      // 注册Runtime系统
      DomainRegistry.register({
        name: 'RuntimeSystem',
        priority: DomainRegistry.Priority.RUNTIME,
        init: () => {
          logger.info('[DomainBootstrap] Starting Runtime system...');
          startRuntime();
          return Promise.resolve();
        }
      });

      // 0. 遗留表初始化 (LEGACY)
      DomainRegistry.register({
        name: 'LegacyTables',
        priority: DomainRegistry.Priority.CORE_INFRA,
        init: () => LegacyTableInitializer.init()
      });

      // 1. 核心基础设施 (CORE_INFRA)
    DomainRegistry.register({
      name: 'FeatureGovernance',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => FeatureGovernanceService.initTable()
    });
    DomainRegistry.register({
      name: 'QuotaGovernance',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => QuotaGovernanceService.initTable()
    });
    DomainRegistry.register({
      name: 'AuthService',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => AuthService.initTable()
    });
    DomainRegistry.register({
      name: 'TurboGateway',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => TurboGateway.init()
    });
    DomainRegistry.register({
      name: 'CreativeService',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => CreativeService.initTable()
    });
    DomainRegistry.register({
      name: 'TenantService',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => TenantService.initTable()
    });
    DomainRegistry.register({
      name: 'DomainEventBus',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => { DomainEventBus.getInstance(); return Promise.resolve(); }
    });
    DomainRegistry.register({
      name: 'AuditService',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => AuditService.initTable()
    });
    DomainRegistry.register({
      name: 'ActionAudit',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => ActionAuditService.initTable()
    });
    DomainRegistry.register({
      name: 'BillingService',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => BillingService.initTable()
    });
    DomainRegistry.register({
      name: 'BillingEngine',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => BillingEngine.initTable()
    });

    // 1.5 治理与遥测 (TELEMETRY & GOVERNANCE - AI-2)
    DomainRegistry.register({
      name: 'AutoDiagnostics',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => AutoDiagnosticsService.initTable()
    });
    DomainRegistry.register({
      name: 'CostAttribution',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => CostAttributionService.initTable()
    });
    DomainRegistry.register({
      name: 'TracingTopo',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => TracingTopoService.initTable()
    });
    DomainRegistry.register({
      name: 'SemanticLog',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => SemanticLogService.initTable()
    });
    DomainRegistry.register({
      name: 'PredictiveHealth',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => PredictiveHealthService.initTable()
    });
    DomainRegistry.register({
      name: 'QuotaCircuitBreaker',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => QuotaCircuitBreakerService.initTable()
    });
    DomainRegistry.register({
      name: 'RedTeaming',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => RedTeamingService.initTable()
    });
    DomainRegistry.register({
      name: 'DataCompliance',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => DataComplianceService.initTable()
    });
    DomainRegistry.register({
      name: 'BehavioralRisk',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => BehavioralRiskService.initTable()
    });
    DomainRegistry.register({
      name: 'VisualSourcing',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve() // No table init needed, uses sourcing_audit
    });
    DomainRegistry.register({
      name: 'SLAScoring',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve() // No table init needed, uses sourcing_audit
    });
    DomainRegistry.register({
      name: 'DisputeAdvisor',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => DisputeAdvisorService.initTable()
    });
    DomainRegistry.register({
      name: 'FraudShared',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => FraudSharedService.initTable()
    });
    DomainRegistry.register({
      name: 'TrustEvolution',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'DisputeClassifier',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'OmniStock',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => OmniStockService.initTable()
    });
    DomainRegistry.register({
      name: 'ProductHealth',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => ProductHealthService.initTable()
    });
    DomainRegistry.register({
      name: 'GreenSupply',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'HolidayRisk',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'PackingOptimizer',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'StuckTracking',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'InvoiceLateRisk',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'ContentGap',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'GlobalCSMonitor',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'ProcurementAudit',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'SensibleStock',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'PaymentRisk',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'DynamicShipping',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'LeadTimeDrift',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'ChannelStatus',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => ChannelStatusService.initTable()
    });
    DomainRegistry.register({
      name: 'InventoryAging',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'SupplierRiskRadar',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'PlatformFeeWatcher',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'StyleWar',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'SeaFreightAdvisor',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'AutoRCA',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({      name: 'FeatureToggle',      priority: DomainRegistry.Priority.CORE_INFRA,      init: () => FeatureToggleService.initTable()    });    DomainRegistry.register({      name: 'DBSharding',      priority: DomainRegistry.Priority.CORE_INFRA,      init: () => DBShardingService.initTable()    });    DomainRegistry.register({      name: 'EnvValidator',      priority: DomainRegistry.Priority.CORE_INFRA,      init: () => EnvValidatorService.validate()    });    DomainRegistry.register({
      name: 'SecurityScan',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => SecurityScanService.init()
    });
    DomainRegistry.register({
      name: 'CacheConsistency',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => CacheConsistencyService.init()
    });
    DomainRegistry.register({
      name: 'PermissionAudit',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => PermissionAuditService.init()
    });
    DomainRegistry.register({
      name: 'ContainerSecurity',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => ContainerSecurityService.init()
    });
    DomainRegistry.register({
      name: 'S3Quota',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => S3QuotaManager.initTable()
    });
    DomainRegistry.register({
      name: 'TXAudit',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => TransactionScopeService.init()
    });
    DomainRegistry.register({
      name: 'MemoryWatchdog',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => MemoryWatchdogService.init()
    });
    DomainRegistry.register({
      name: 'ContainerQuota',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => ContainerQuotaService.init()
    });
    DomainRegistry.register({
      name: 'GlobalTracing',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'DeadlockAdvisor',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => DeadlockAdvisor.init()
    });
    DomainRegistry.register({
      name: 'DLQMonitor',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => BullMQDeadLetterService.init()
    });
    DomainRegistry.register({
      name: 'WorkerProfiler',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => WorkerProfilerService.init()
    });

    DomainRegistry.register({
      name: 'AutonomousSandbox',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => AutonomousSandboxService.initTable()
    });
    DomainRegistry.register({
      name: 'FederatedNode',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => FederatedNodeService.initTable()
    });
    DomainRegistry.register({
      name: 'P2PConnection',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => P2PConnectionService.init()
    });
    DomainRegistry.register({
      name: 'SovereignReputationV2',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => SovereignReputationV2Service.initTable()
    });
    DomainRegistry.register({
      name: 'AutoCircuitBreaker',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => AutoCircuitBreakerService.initTable()
    });
    DomainRegistry.register({
      name: 'SandboxROIAdvisor',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'AgentSwarm',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => AgentSwarmService.initTable()
    });
    DomainRegistry.register({
      name: 'SKUMapper',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => SKUMappingService.initTable()
    });
    DomainRegistry.register({
      name: 'UnifiedTaskHub',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => UnifiedTaskService.initTable()
    });
    DomainRegistry.register({
      name: 'ExperimentHub',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => ExperimentService.initTable()
    });

    // 2. 运行时与安全 (SECURITY / RUNTIME)
    DomainRegistry.register({
      name: 'PriorityAsyncEngine',
      priority: DomainRegistry.Priority.RUNTIME,
      init: () => { PriorityAsyncEngine.startProcessor(); return Promise.resolve(); }
    });
    DomainRegistry.register({
      name: 'V2MigrationAdvisor',
      priority: DomainRegistry.Priority.RUNTIME,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'DocsSync',
      priority: DomainRegistry.Priority.RUNTIME,
      init: () => TsoaDocGenerator.init()
    });
    DomainRegistry.register({
      name: 'EventBus',
      priority: DomainRegistry.Priority.RUNTIME,
      init: () => EventBusOptimizationService.init()
    });
    DomainRegistry.register({
      name: 'PrivateInventorySync',
      priority: DomainRegistry.Priority.RUNTIME,
      init: () => Promise.resolve()
    });    DomainRegistry.register({      name: 'SnowflakeID',      priority: DomainRegistry.Priority.RUNTIME,      init: () => SnowflakeIDService.init()    });    DomainRegistry.register({      name: 'PluginManager',      priority: DomainRegistry.Priority.RUNTIME,      init: () => { PluginManager.loadPlugins(); return Promise.resolve(); }    });    DomainRegistry.register({      name: 'Warmup',      priority: DomainRegistry.Priority.SUPPORT,      init: () => WarmupService.init()    });    DomainRegistry.register({      name: 'SSLWatch',      priority: DomainRegistry.Priority.SECURITY,      init: () => CertsMonitorService.init()    });
    DomainRegistry.register({
      name: 'LogMasking',
      priority: DomainRegistry.Priority.SECURITY,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'SecureVault',
      priority: DomainRegistry.Priority.SECURITY,
      init: () => Promise.resolve()
    });
    DomainRegistry.register({
      name: 'VaultService',
      priority: DomainRegistry.Priority.SECURITY,
      init: () => VaultService.initTable()
    });
    DomainRegistry.register({
      name: 'DIDHandshake',
      priority: DomainRegistry.Priority.SECURITY,
      init: () => DIDHandshakeService.initTable()
    });
    DomainRegistry.register({
      name: 'PrivateAudit',
      priority: DomainRegistry.Priority.SECURITY,
      init: () => PrivateAuditService.initTable()
    });
    DomainRegistry.register({
      name: 'WebhookService',
      priority: DomainRegistry.Priority.SECURITY,
      init: () => WebhookService.initTable()
    });

    // 3. 业务领域 (BIZ_DOMAIN)
    DomainRegistry.register({
      name: 'ProductService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => ProductService.initTable()
    });
    DomainRegistry.register({
      name: 'ConsumerOrderService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => ConsumerOrderService.initTable()
    });
    DomainRegistry.register({
      name: 'SyncService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SyncService.initTable()
    });
    DomainRegistry.register({
      name: 'WarehouseService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => WarehouseService.initTable()
    });
    DomainRegistry.register({
      name: 'Onboarding',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => OnboardingService.initTable()
    });
    DomainRegistry.register({
      name: 'SLAGovernance',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SLAGovernanceService.initTable()
    });
    DomainRegistry.register({
      name: 'CustomerService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CustomerService.initTable()
    });
    DomainRegistry.register({
      name: 'SupportService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SupportService.initTable()
    });
    DomainRegistry.register({
      name: 'CreativeService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CreativeService.initTable()
    });

    // AI-3 Business Domains (Finance)
    DomainRegistry.register({
      name: 'ReconciliationService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => ReconciliationService.initTable()
    });
    DomainRegistry.register({
      name: 'SKUEfficiencyService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SKUEfficiencyService.initTable()
    });
    DomainRegistry.register({
      name: 'PayoutCycleService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => PayoutCycleService.initTable()
    });
    DomainRegistry.register({
      name: 'InventoryValueService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => InventoryValueService.initTable()
    });
    DomainRegistry.register({
      name: 'RefundRiskService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => RefundRiskService.initTable()
    });
    DomainRegistry.register({
      name: 'PaymentTermsService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => PaymentTermsService.initTable()
    });
    DomainRegistry.register({
      name: 'RemitPathService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => RemitPathService.initTable()
    });
    DomainRegistry.register({
      name: 'StockAuditService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => StockAuditService.initTable()
    });
    DomainRegistry.register({
      name: 'AdOptimizerService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => AdOptimizerService.initTable()
    });
    DomainRegistry.register({
      name: 'TenantHealthService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => TenantHealthService.initTable()
    });
    DomainRegistry.register({
      name: 'RebateService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => RebateService.initTable()
    });
    DomainRegistry.register({
      name: 'FinancialAuditService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => FinancialAuditService.initTable()
    });
    DomainRegistry.register({
      name: 'ExpenseService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => ExpenseService.initTable()
    });
    DomainRegistry.register({
      name: 'MaterialRiskService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => MaterialRiskService.initTable()
    });
    DomainRegistry.register({
      name: 'RefundAuditService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => RefundAuditService.initTable()
    });
    DomainRegistry.register({
      name: 'CACThresholdService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CACThresholdService.initTable()
    });
    DomainRegistry.register({
      name: 'CommissionService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CommissionService.initTable()
    });
    DomainRegistry.register({
      name: 'TurnoverWatchdog',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => TurnoverWatchdog.initTable()
    });
    DomainRegistry.register({
      name: 'WarrantyService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => WarrantyService.initTable()
    });
    DomainRegistry.register({
      name: 'TaxFilingAdvisor',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => TaxFilingAdvisor.initTable()
    });
    DomainRegistry.register({
      name: 'MarginProtector',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => MarginProtector.initTable()
    });
    DomainRegistry.register({
      name: 'CreditMonitorService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CreditMonitorService.initTable()
    });
    DomainRegistry.register({
      name: 'LegalTaxService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => LegalTaxService.initTable()
    });
    DomainRegistry.register({
      name: 'PayoutOptimizer',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => PayoutOptimizer.initTable()
    });
    DomainRegistry.register({
      name: 'FinancialPlanner',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => FinancialPlanner.initTable()
    });
    DomainRegistry.register({
      name: 'OrderProfitService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => OrderProfitService.initTable()
    });
    DomainRegistry.register({
      name: 'CurrencyRiskService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CurrencyRiskService.initTable()
    });
    DomainRegistry.register({
      name: 'PricingAuditService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => PricingAuditService.initTable()
    });
    DomainRegistry.register({
      name: 'TrueROASService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => TrueROASService.initTable()
    });
    DomainRegistry.register({
      name: 'TaxComplianceService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => TaxComplianceService.initTable()
    });
    DomainRegistry.register({
      name: 'VendorCreditService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => VendorCreditService.initTable()
    });
    DomainRegistry.register({
      name: 'CashflowPredictor',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CashflowPredictor.initTable()
    });
    DomainRegistry.register({
      name: 'BillingEngine',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => BillingEngine.initTable()
    });
    DomainRegistry.register({
      name: 'MultiAssetSettlementService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => MultiAssetSettlementService.initTable()
    });
    DomainRegistry.register({
      name: 'SovereignCreditPoolService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SovereignCreditPoolService.initTable()
    });
    DomainRegistry.register({
      name: 'InvoicingService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => InvoicingService.initTable()
    });
    DomainRegistry.register({
      name: 'CashflowForecastService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CashflowForecastService.initTable()
    });
    DomainRegistry.register({
      name: 'PoolSourcingService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => PoolSourcingService.initTable()
    });
    DomainRegistry.register({
      name: 'SovereignWealthFundService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SovereignWealthFundService.initTable()
    });
    DomainRegistry.register({
      name: 'CommodityHedgingService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CommodityHedgingService.initTable()
    });
    DomainRegistry.register({
      name: 'TaxPlanningService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => TaxPlanningService.initTable()
    });

    // AI-3 Business Domains (Logistics)
    DomainRegistry.register({
      name: 'LogisticTelemetryService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => LogisticTelemetryService.initTable()
    });
    DomainRegistry.register({
      name: 'FreightAuditService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => FreightAuditService.initTable()
    });
    DomainRegistry.register({
      name: 'LastMileAIService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => LastMileAIService.initTable()
    });
    DomainRegistry.register({
      name: 'LastMileOptimizerService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => LastMileOptimizerService.initTable()
    });
    DomainRegistry.register({
      name: 'RouteOptimizerService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => RouteOptimizerService.initTable()
    });
    DomainRegistry.register({
      name: 'CongestionFailoverService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => CongestionFailoverService.initTable()
    });
    DomainRegistry.register({
      name: 'SupplierScoringService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SupplierScoringService.initTable()
    });
    DomainRegistry.register({
      name: 'GlobalFulfillmentService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => GlobalFulfillmentService.initTable()
    });
    DomainRegistry.register({
      name: 'TradeService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => TradeService.initTable()
    });

    // AI-3 Business Domains (Marketing/Trade)
    DomainRegistry.register({
      name: 'KOLService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => KOLService.initTable()
    });
    DomainRegistry.register({
      name: 'MarketingCalendarService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => MarketingCalendarService.initTable()
    });
    DomainRegistry.register({
      name: 'SocialPulseService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SocialPulseService.initTable()
    });
    DomainRegistry.register({
      name: 'InventoryForecastService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => InventoryForecastService.initTable()
    });
    DomainRegistry.register({
      name: 'SovereignCrisisService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SovereignCrisisService.initTable()
    });

    // 4. 重型 AGI 模块 (AGI_HEAVY) - 标记 isAgi: true
    DomainRegistry.register({
      name: 'BusinessModelEvolution',
      priority: DomainRegistry.Priority.AGI_HEAVY,
      isAgi: true,
      init: () => BusinessModelEvolutionService.initTable()
    });
    DomainRegistry.register({
      name: 'DecisionExplainability',
      priority: DomainRegistry.Priority.AGI_HEAVY,
      isAgi: true,
      init: () => DecisionExplainabilityEngine.initTable()
    });




    DomainRegistry.register({
      name: 'ChatBotService',
      priority: DomainRegistry.Priority.AGI_HEAVY,
      isAgi: true,
      init: () => ChatBotService.initTable()
    });
    DomainRegistry.register({
      name: 'ImageRecognitionService',
      priority: DomainRegistry.Priority.AGI_HEAVY,
      isAgi: true,
      init: () => ImageRecognitionService.initTable()
    });
    DomainRegistry.register({
      name: 'NaturalLanguageProcessingService',
      priority: DomainRegistry.Priority.AGI_HEAVY,
      isAgi: true,
      init: () => NaturalLanguageProcessingService.initTable()
    });
    DomainRegistry.register({
      name: 'RecommendationService',
      priority: DomainRegistry.Priority.AGI_HEAVY,
      isAgi: true,
      init: () => RecommendationService.initTable()
    });

    // 5. 支撑模块 (SUPPORT)
    DomainRegistry.register({
      name: 'SovereignCarbon',
      priority: DomainRegistry.Priority.SUPPORT,
      init: () => SovereignCarbonService.initTable()
    });
    DomainRegistry.register({
      name: 'SelfHealing',
      priority: DomainRegistry.Priority.SUPPORT,
      init: () => SelfHealingService.initTable()
    });
    DomainRegistry.register({
      name: 'ReviewService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => ReviewService.initTable()
    });
    DomainRegistry.register({
      name: 'DisputeResolverService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => DisputeResolverService.initTable()
    });
    DomainRegistry.register({
      name: 'DynamicPricingService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => Promise.resolve() // DynamicPricingService 不需要初始化表
    });
    DomainRegistry.register({
      name: 'SupplierService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => SupplierService.initTable()
    });
    DomainRegistry.register({
      name: 'MediaAssetService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => MediaAssetService.initTable()
    });
    DomainRegistry.register({
      name: 'FinanceReconciliationService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => FinanceReconciliationService.initTable()
    });
    DomainRegistry.register({
      name: 'ReportService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => ReportService.initTable()
    });
    DomainRegistry.register({
      name: 'RBACService',
      priority: DomainRegistry.Priority.CORE_INFRA,
      init: () => RBACService.initTable()
    });
    DomainRegistry.register({
      name: 'FeatureActivationService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => FeatureActivationService.initTable()
    });
    DomainRegistry.register({
      name: 'MerchantSettlementService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => MerchantSettlementService.initTable()
    });
    DomainRegistry.register({
      name: 'MerchantProductOrderService',
      priority: DomainRegistry.Priority.BIZ_DOMAIN,
      init: () => MerchantProductOrderService.initTable()
    });

    // 执行全量 Bootstrap
    await DomainRegistry.bootstrap();
    } catch (error) {
      logger.error(`[DomainBootstrap] Error during initialization: ${(error as any).message}`);
      // 即使初始化失败，也继续执行，确保应用能够启动
      logger.warn('[DomainBootstrap] Continuing despite initialization errors');
    }
  }
}
