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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
const EventEmitter = require('events');

/**
 * Service Container for dependency injection and service lifecycle management
 */
class ServiceContainer extends EventEmitter {
  constructor() {
    super();
    this.services = new Map();
    this.factories = new Map();
    this.singletons = new Map();
    this.circuitBreakers = new Map();
    this.isShuttingDown = false;
    this.startupOrder = [];
    this.shutdownOrder = [];
  }

  /**
   * Register a service factory
   * @param {string} name - Service name
   * @param {Function} factory - Factory function that creates the service
   * @param {Object} options - Registration options
   * @param {boolean} options.singleton - Whether to create only one instance
   * @param {Array<string>} options.dependencies - List of service dependencies
   * @param {Object} options.circuitBreaker - Circuit breaker configuration
   * @param {number} options.startupPriority - Startup priority (lower = earlier)
   * @param {Function} options.healthCheck - Health check function
   */
  register(name, factory, options = {}) {
    if (this.factories.has(name)) {
      throw new Error(`Service '${name}' is already registered`);
    }

    const serviceConfig = {
      factory,
      singleton: options.singleton || false,
      dependencies: options.dependencies || [],
      circuitBreaker: options.circuitBreaker || null,
      startupPriority: options.startupPriority || 100,
      healthCheck: options.healthCheck || null,
      startupTimeout: options.startupTimeout || 30000,
      shutdownTimeout: options.shutdownTimeout || 10000
    };

    this.factories.set(name, serviceConfig);

    // Add to startup order based on priority
    this.updateStartupOrder(name, serviceConfig.startupPriority);

    console.log(`Service '${name}' registered${serviceConfig.singleton ? ' (singleton)' : ''}`);
  }

  /**
   * Register a service instance directly
   * @param {string} name - Service name
   * @param {*} instance - Service instance
   * @param {Object} options - Registration options
   */
  registerInstance(name, instance, options = {}) {
    if (this.services.has(name)) {
      throw new Error(`Service instance '${name}' is already registered`);
    }

    this.services.set(name, instance);

    if (options.healthCheck) {
      this.factories.set(name, { healthCheck: options.healthCheck });
    }

    console.log(`Service instance '${name}' registered`);
  }

  /**
   * Get a service instance
   * @param {string} name - Service name
   * @returns {*} Service instance
   */
  get(name) {
    if (this.isShuttingDown) {
      throw new Error(`Cannot get service '${name}' during shutdown`);
    }

    // Return existing instance if available
    if (this.services.has(name)) {
      return this.services.get(name);
    }

    // Return singleton if available
    if (this.singletons.has(name)) {
      return this.singletons.get(name);
    }

    // Create new instance
    return this.createInstance(name);
  }

  /**
   * Create a new service instance
   * @param {string} name - Service name
   * @returns {*} Service instance
   */
  createInstance(name) {
    const serviceConfig = this.factories.get(name);
    if (!serviceConfig) {
      throw new Error(`Service '${name}' is not registered`);
    }

    // Check for circular dependencies
    this.checkCircularDependencies(name, new Set());

    // Create dependency instances
    const dependencies = {};
    for (const depName of serviceConfig.dependencies) {
      dependencies[depName] = this.get(depName);
    }

    // Create service instance
    let instance;
    try {
      instance = serviceConfig.factory(dependencies, this);
    } catch (error) {
      console.error(`Failed to create service '${name}':`, error);
      throw new Error(`Service creation failed for '${name}': ${error.message}`);
    }

    // Store instance
    if (serviceConfig.singleton) {
      this.singletons.set(name, instance);
    } else {
      this.services.set(name, instance);
    }

    // Set up circuit breaker if configured
    if (serviceConfig.circuitBreaker) {
      this.setupCircuitBreaker(name, instance, serviceConfig.circuitBreaker);
    }

    this.emit('serviceCreated', { name, instance, config: serviceConfig });
    console.log(`Service '${name}' created successfully`);

    return instance;
  }

  /**
   * Check for circular dependencies
   * @param {string} name - Service name
   * @param {Set} visited - Set of visited services
   */
  checkCircularDependencies(name, visited) {
    if (visited.has(name)) {
      const cycle = Array.from(visited).concat(name).join(' -> ');
      throw new Error(`Circular dependency detected: ${cycle}`);
    }

    const serviceConfig = this.factories.get(name);
    if (!serviceConfig) return;

    visited.add(name);

    for (const depName of serviceConfig.dependencies) {
      this.checkCircularDependencies(depName, new Set(visited));
    }
  }

  /**
   * Set up circuit breaker for a service
   * @param {string} name - Service name
   * @param {*} instance - Service instance
   * @param {Object} circuitBreakerConfig - Circuit breaker configuration
   */
  setupCircuitBreaker(name, instance, circuitBreakerConfig) {
    const CircuitBreaker = require('./CircuitBreaker');

    const breaker = new CircuitBreaker({
      name: `Service_${name}`,
      ...circuitBreakerConfig,
      onStateChange: (event) => {
        this.emit('circuitBreakerStateChange', { serviceName: name, ...event });
      },
      onFailure: (event) => {
        this.emit('circuitBreakerFailure', { serviceName: name, ...event });
      }
    });

    this.circuitBreakers.set(name, breaker);

    // Wrap service methods with circuit breaker
    this.wrapServiceWithCircuitBreaker(instance, breaker);
  }

  /**
   * Wrap service methods with circuit breaker
   * @param {*} instance - Service instance
   * @param {CircuitBreaker} breaker - Circuit breaker instance
   */
  wrapServiceWithCircuitBreaker(instance, breaker) {
    const methodsToWrap = ['execute', 'run', 'process', 'analyze', 'generate'];

    for (const methodName of methodsToWrap) {
      if (typeof instance[methodName] === 'function') {
        const originalMethod = instance[methodName].bind(instance);
        instance[methodName] = (...args) => {
          return breaker.execute(originalMethod, ...args);
        };
      }
    }
  }

  /**
   * Check if a service is registered
   * @param {string} name - Service name
   * @returns {boolean} Registration status
   */
  has(name) {
    return this.factories.has(name) || this.services.has(name);
  }

  /**
   * Update startup order based on priority
   * @param {string} name - Service name
   * @param {number} priority - Startup priority
   */
  updateStartupOrder(name, priority) {
    // Remove if already exists
    const existingIndex = this.startupOrder.findIndex(item => item.name === name);
    if (existingIndex !== -1) {
      this.startupOrder.splice(existingIndex, 1);
    }

    // Insert at correct position based on priority
    const insertIndex = this.startupOrder.findIndex(item => item.priority > priority);
    if (insertIndex === -1) {
      this.startupOrder.push({ name, priority });
    } else {
      this.startupOrder.splice(insertIndex, 0, { name, priority });
    }

    // Update shutdown order (reverse of startup)
    this.shutdownOrder = [...this.startupOrder].reverse();
  }

  /**
   * Start all services in priority order
   */
  async startServices() {
    console.log('Starting services...');

    for (const { name } of this.startupOrder) {
      const serviceConfig = this.factories.get(name);
      if (!serviceConfig) continue;

      try {
        console.log(`Starting service: ${name}`);

        // Create instance with timeout
        const instance = await this.createInstanceWithTimeout(name, serviceConfig.startupTimeout);

        // Call startup method if it exists
        if (instance && typeof instance.startup === 'function') {
          await instance.startup();
        }

        this.emit('serviceStarted', { name, instance });
        console.log(`Service '${name}' started successfully`);

      } catch (error) {
        console.error(`Failed to start service '${name}':`, error);
        this.emit('serviceStartFailed', { name, error });

        // Decide whether to continue or stop based on service criticality
        if (serviceConfig.critical !== false) {
          throw new Error(`Critical service '${name}' failed to start: ${error.message}`);
        }
      }
    }

    console.log('All services started successfully');
  }

  /**
   * Create service instance with timeout
   * @param {string} name - Service name
   * @param {number} timeout - Timeout in milliseconds
   * @returns {Promise<*>} Service instance
   */
  async createInstanceWithTimeout(name, timeout) {
    return Promise.race([
      Promise.resolve(this.createInstance(name)),
      new Promise((_, reject) => {
        setTimeout(() => reject(new Error(`Service startup timeout`)), timeout);
      })
    ]);
  }

  /**
   * Shutdown all services in reverse order
   */
  async shutdown() {
    if (this.isShuttingDown) {
      return;
    }

    console.log('Shutting down services...');
    this.isShuttingDown = true;

    for (const { name } of this.shutdownOrder) {
      try {
        await this.shutdownService(name);
      } catch (error) {
        console.error(`Error shutting down service '${name}':`, error);
      }
    }

    // Stop circuit breakers
    for (const [name, breaker] of this.circuitBreakers) {
      try {
        breaker.stopMonitoring();
      } catch (error) {
        console.error(`Error stopping circuit breaker for '${name}':`, error);
      }
    }

    this.services.clear();
    this.singletons.clear();
    this.circuitBreakers.clear();

    console.log('All services shut down');
    this.emit('shutdown');
  }

  /**
   * Shutdown a specific service
   * @param {string} name - Service name
   */
  async shutdownService(name) {
    const instance = this.services.get(name) || this.singletons.get(name);
    if (!instance) return;

    const serviceConfig = this.factories.get(name);
    const timeout = serviceConfig?.shutdownTimeout || 10000;

    console.log(`Shutting down service: ${name}`);

    try {
      // Call shutdown method if it exists
      if (typeof instance.shutdown === 'function') {
        await Promise.race([
          instance.shutdown(),
          new Promise((_, reject) => {
            setTimeout(() => reject(new Error('Shutdown timeout')), timeout);
          })
        ]);
      }

      this.emit('serviceShutdown', { name, instance });
      console.log(`Service '${name}' shut down successfully`);

    } catch (error) {
      console.error(`Error during shutdown of service '${name}':`, error);
      this.emit('serviceShutdownError', { name, error });
    }

    // Remove from containers
    this.services.delete(name);
    this.singletons.delete(name);
  }

  /**
   * Get health status of all services
   */
  async getHealthStatus() {
    const healthStatus = {
      status: 'healthy',
      services: {},
      circuitBreakers: {},
      summary: {
        total: 0,
        healthy: 0,
        unhealthy: 0,
        unknown: 0
      }
    };

    // Check service health
    for (const [name, serviceConfig] of this.factories) {
      const instance = this.services.get(name) || this.singletons.get(name);

      if (!instance) {
        healthStatus.services[name] = { status: 'not_started' };
        healthStatus.summary.unknown++;
        continue;
      }

      healthStatus.summary.total++;

      try {
        let serviceHealth = { status: 'healthy' };

        // Use custom health check if available
        if (serviceConfig.healthCheck) {
          serviceHealth = await serviceConfig.healthCheck(instance);
        } else if (typeof instance.getHealthStatus === 'function') {
          serviceHealth = await instance.getHealthStatus();
        } else if (typeof instance.healthCheck === 'function') {
          serviceHealth = await instance.healthCheck();
        }

        healthStatus.services[name] = serviceHealth;

        if (serviceHealth.status === 'healthy') {
          healthStatus.summary.healthy++;
        } else {
          healthStatus.summary.unhealthy++;
        }

      } catch (error) {
        healthStatus.services[name] = {
          status: 'unhealthy',
          error: error.message
        };
        healthStatus.summary.unhealthy++;
      }
    }

    // Check circuit breaker status
    for (const [name, breaker] of this.circuitBreakers) {
      healthStatus.circuitBreakers[name] = breaker.getStatus();
    }

    // Determine overall status
    if (healthStatus.summary.unhealthy > 0) {
      healthStatus.status = 'unhealthy';
    } else if (healthStatus.summary.unknown > 0) {
      healthStatus.status = 'degraded';
    }

    return healthStatus;
  }

  /**
   * Get service statistics
   */
  getStats() {
    return {
      registered: this.factories.size,
      active: this.services.size,
      singletons: this.singletons.size,
      circuitBreakers: this.circuitBreakers.size,
      startupOrder: this.startupOrder.map(item => item.name),
      isShuttingDown: this.isShuttingDown
    };
  }

  /**
   * List all registered services
   */
  listServices() {
    const services = [];

    for (const [name, config] of this.factories) {
      const isActive = this.services.has(name) || this.singletons.has(name);
      const hasCircuitBreaker = this.circuitBreakers.has(name);

      services.push({
        name,
        isActive,
        isSingleton: config.singleton,
        hasCircuitBreaker,
        dependencies: config.dependencies,
        startupPriority: config.startupPriority
      });
    }

    return services.sort((a, b) => a.startupPriority - b.startupPriority);
  }
}

// Create singleton instance
const serviceContainer = new ServiceContainer();

module.exports = serviceContainer;