# Enhanced Message Processing Architecture Plan

## Architecture Overview

```
┌─────────────┐    ┌──────────────────┐    ┌────────────────────────┐    ┌─────────────────┐
│   Device    │───▶│ Channel Listener │───▶│ IncomingMessageProcessor│───▶│ UpstreamRouter  │
│             │    │  (Port:Packager) │    │                        │    │                 │
│             │    │                  │    │                        │    │                 │
│             │◄───┤ Response Handler │◄───┤ Response Processing    │◄───┤ Network Response│
└─────────────┘    └──────────────────┘    └────────────────────────┘    └─────────────────┘
                            │                                                        │
                   ┌─────────────────┐                                    ┌─────────────────┐
                   │ Channel Config  │                                    │ Upstream Config │
                   │ Port→Packager   │                                    │ Route→Host:Port │
                   └─────────────────┘                                    └─────────────────┘
```

## Key Components

### 1. **Channel Configuration System**
- **Port-to-Packager Mapping**: Each listener port uses specific packager
- **Device-Specific Handling**: Different devices can use different packagers
- **TPDU Management**: Channel-specific TPDU handling

### 2. **IncomingMessageProcessor** (Central Hub)
- **Receives**: Unpacked ISOMsg from channel listeners
- **Processes**: Business logic, validation, enrichment
- **Routes**: To appropriate upstream networks
- **Handles Responses**: Processes upstream responses back to devices

### 3. **UpstreamRouter** (Configurable Routing)
- **Network Selection**: Route based on BIN, MTI, channel
- **Host:Port Mapping**: Configurable upstream destinations
- **Packager Selection**: Use appropriate packager for each network
- **Failover Support**: Handle network failures

### 4. **Response Processing Pipeline**
- **Response Transformation**: Convert upstream responses for devices
- **Channel-Specific Formatting**: Use correct packager for response
- **Error Handling**: Consistent error response generation

## Configuration Structure

### Channel Configuration
```elixir
config :da_product_app, :channels,
  listeners: [
    %{
      port: 8080,
      packager: DaProductApp.MercuryISO8583.Packagers.ISO87BPackager,
      tpdu_patterns: [...],
      description: "Mercury Device Channel"
    },
    %{
      port: 8081, 
      packager: DaProductApp.MercuryISO8583.Packagers.NetworkPackagers.VisaPackager,
      description: "VISA Direct Channel"
    }
  ]
```

### Upstream Routing Configuration
```elixir
config :da_product_app, :upstream_routing,
  networks: [
    %{
      name: :visa,
      host: "visa.network.com",
      port: 9001,
      packager: DaProductApp.MercuryISO8583.Packagers.NetworkPackagers.VisaPackager,
      routing_rules: [
        %{bin_range: "4000000000000000..4999999999999999", priority: 1}
      ]
    },
    %{
      name: :mastercard,
      host: "mastercard.network.com", 
      port: 9002,
      packager: DaProductApp.MercuryISO8583.Packagers.NetworkPackagers.MastercardPackager,
      routing_rules: [
        %{bin_range: "5000000000000000..5999999999999999", priority: 1}
      ]
    }
  ]
```

## Benefits of New Architecture

### 1. **Separation of Concerns**
- **Channel Management**: Handle device-specific protocols
- **Message Processing**: Business logic and validation
- **Upstream Routing**: Network communication
- **Response Handling**: Device-specific responses

### 2. **Configurability**
- **Dynamic Channel Setup**: Add new device types without code changes
- **Flexible Routing**: Configure upstream networks via config
- **Packager Selection**: Use appropriate packagers per channel/network

### 3. **Scalability**
- **Independent Components**: Scale each component separately
- **Connection Pooling**: Manage upstream connections efficiently
- **Load Balancing**: Distribute messages across multiple upstreams

### 4. **Maintainability**
- **Pure ISOMsg Flow**: Consistent data structures throughout
- **No Legacy Code**: Remove all map-based processing
- **Clear Error Handling**: Consistent error management

## Implementation Phases

### **Phase 1: Core Infrastructure**
1. Create `IncomingMessageProcessor`
2. Create `UpstreamRouter` with configuration
3. Create `ChannelManager` for port management

### **Phase 2: Configuration System**
1. Channel configuration loading
2. Upstream routing configuration
3. Dynamic configuration updates

### **Phase 3: Integration**
1. Update Protocol to use IncomingMessageProcessor
2. Remove legacy code paths
3. Implement response processing pipeline

### **Phase 4: Testing & Optimization**
1. End-to-end testing
2. Performance optimization
3. Error scenario validation

## Migration Strategy

### **Backward Compatibility**
- Keep existing Protocol.ex during transition
- Gradual migration of channels to new architecture
- Feature flags for switching between old/new systems

### **Configuration Migration**
- Convert existing routing rules to new format
- Update channel configurations
- Test with existing connections

### **Validation**
- Compare old vs new message processing
- Validate upstream routing accuracy
- Test error handling scenarios

This architecture provides enterprise-grade message processing with clear separation of concerns, configurability, and maintainability while leveraging your pack/unpack implementation.
