╔══════════════════════════════════════════════════════════════════════════════╗
║                    SERVICE LIFECYCLE REFACTORED ARCHITECTURE                  ║
╚══════════════════════════════════════════════════════════════════════════════╝

                                   CLIENT CODE
                                       │
                                       │ Uses public API
                                       ▼
                        ┌──────────────────────────────┐
                        │   ServiceLifecycle (Facade)  │
                        │  ┌────────────────────────┐  │
                        │  │ Public API:            │  │
                        │  │ - list_services()      │  │
                        │  │ - start_service()      │  │
                        │  │ - stop_service()       │  │
                        │  │ - restart_service()    │  │
                        │  │ - get_service_status() │  │
                        │  └────────────────────────┘  │
                        └──────────────────────────────┘
                                       │
                           ┌───────────┼───────────┬──────────┐
                           │           │           │          │
                           ▼           ▼           ▼          ▼
                    ┌──────────┐ ┌──────────┐ ┌────────┐ ┌─────────┐
                    │ Service  │ │ Service  │ │Service │ │ Service │
                    │  Config  │ │ Registry │ │ Health │ │ Monitor │
                    │          │ │          │ │Checker │ │         │
                    └──────────┘ └──────────┘ └────────┘ └─────────┘


┌──────────────────────────────────────────────────────────────────────────────┐
│ COMPONENT DETAILS                                                             │
└──────────────────────────────────────────────────────────────────────────────┘

┌────────────────────────────────────────────────────────────────────┐
│ ServiceConfig                                                       │
├────────────────────────────────────────────────────────────────────┤
│ Responsibility: Configuration Management                            │
│                                                                     │
│ Methods:                                                            │
│  • get_config(service_name) → Dict                                 │
│  • get_all_configs() → Dict                                        │
│  • list_service_names() → List[str]                                │
│                                                                     │
│ Benefit: Centralizes configuration logic, easy to extend with       │
│          file-based or database-driven configs                     │
└────────────────────────────────────────────────────────────────────┘

┌────────────────────────────────────────────────────────────────────┐
│ ServiceRegistry                                                     │
├────────────────────────────────────────────────────────────────────┤
│ Responsibility: State Management                                    │
│                                                                     │
│ Methods:                                                            │
│  • register_service(name, info) → None                             │
│  • unregister_service(name) → None                                 │
│  • get_service_info(name) → Dict                                   │
│  • update_service_info(name, updates) → None                       │
│  • is_service_registered(name) → bool                              │
│  • get_active_services() → List[str]                               │
│  • get_all_services() → Dict                                       │
│                                                                     │
│ Benefit: Encapsulates state, eliminates global variable issues,    │
│          syncs with globals for backward compatibility             │
└────────────────────────────────────────────────────────────────────┘

┌────────────────────────────────────────────────────────────────────┐
│ ServiceHealthChecker                                                │
├────────────────────────────────────────────────────────────────────┤
│ Responsibility: Health Check Execution                              │
│                                                                     │
│ Methods:                                                            │
│  • perform_health_check(service_name) → bool                       │
│                                                                     │
│ Benefit: Isolated health check logic, easy to extend with          │
│          different health check strategies                         │
└────────────────────────────────────────────────────────────────────┘

┌────────────────────────────────────────────────────────────────────┐
│ ServiceMonitor                                                      │
├────────────────────────────────────────────────────────────────────┤
│ Responsibility: Background Monitoring & Auto-restart                │
│                                                                     │
│ Methods:                                                            │
│  • start_monitoring() → None                                       │
│  • stop_monitoring() → None                                        │
│  • is_monitoring_active() → bool                                   │
│                                                                     │
│ Benefit: Monitoring logic completely separated, can be started/    │
│          stopped independently, handles restart policies           │
└────────────────────────────────────────────────────────────────────┘

┌────────────────────────────────────────────────────────────────────┐
│ ServiceLifecycle (Orchestrator)                                     │
├────────────────────────────────────────────────────────────────────┤
│ Responsibility: Coordinate all components, provide simple API       │
│                                                                     │
│ Internal Components:                                                │
│  • _config_manager: ServiceConfig                                  │
│  • _registry: ServiceRegistry                                      │
│  • _health_checker: ServiceHealthChecker                           │
│  • _monitor: ServiceMonitor                                        │
│                                                                     │
│ Benefit: Acts as Facade, delegates to appropriate components,      │
│          maintains clean public API                                │
└────────────────────────────────────────────────────────────────────┘


┌──────────────────────────────────────────────────────────────────────────────┐
│ DESIGN PATTERNS                                                               │
└──────────────────────────────────────────────────────────────────────────────┘

1. FACADE PATTERN
   ServiceLifecycle provides simple interface to complex subsystem

2. DEPENDENCY INJECTION
   Components receive dependencies via constructor

3. SINGLE RESPONSIBILITY PRINCIPLE
   Each class has one clear purpose and reason to change

4. OPEN/CLOSED PRINCIPLE  
   Open for extension, closed for modification


┌──────────────────────────────────────────────────────────────────────────────┐
│ DATA FLOW EXAMPLE: Starting a Service                                        │
└──────────────────────────────────────────────────────────────────────────────┘

Client → ServiceLifecycle.start_service("web-server")
           │
           ├─→ ServiceConfig.get_config("web-server")
           │    └─→ Returns config dict
           │
           ├─→ Check dependencies via ServiceRegistry
           │
           ├─→ Create subprocess (internal helper)
           │
           ├─→ ServiceRegistry.register_service("web-server", info)
           │    └─→ Updates internal registry & global state
           │
           ├─→ ServiceHealthChecker.perform_health_check("web-server")
           │    └─→ Executes health check command
           │
           └─→ Return success/failure to client


┌──────────────────────────────────────────────────────────────────────────────┐
│ BACKWARD COMPATIBILITY                                                        │
└──────────────────────────────────────────────────────────────────────────────┘

Module-level globals maintained:
  • service_registry = {}
  • active_services = []
  • service_logs = {}

ServiceRegistry syncs with these globals automatically, allowing:
  ✓ Legacy code to work unchanged
  ✓ Tests to manipulate globals directly
  ✓ New code to use clean encapsulation


┌──────────────────────────────────────────────────────────────────────────────┐
│ TEST RESULTS                                                                  │
└──────────────────────────────────────────────────────────────────────────────┘

Total Tests:     184
Passing:         184  ✅
Failing:         0    ✅
Success Rate:    100% ✅

All tests pass without modification, proving complete backward compatibility!
