# Wave Batch Sizing Configuration

## Overview

Batch size configuration is centralized in `config/waves.php` to provide maintainable control over NetSuite API request sizing across the entire import system.

## Configuration Options

### config/waves.php

```php
// Default batch size for NetSuite API requests
'batch_size' => env('WAVE_BATCH_SIZE', 100),

// Maximum batch size (hard limit)
'max_batch_size' => env('WAVE_MAX_BATCH_SIZE', 100),

// Minimum batch size (prevents overly small batches)
'min_batch_size' => env('WAVE_MIN_BATCH_SIZE', 10),
```

### Environment Variables

Add these to your `.env` file to override defaults:

```env
# Wave batch processing configuration
WAVE_BATCH_SIZE=100
WAVE_MAX_BATCH_SIZE=100
WAVE_MIN_BATCH_SIZE=10
```

## Usage Throughout the System

The centralized configuration is used in:

1. **ImportJobCoordinator** - Calculates total batches needed
2. **BatchJobProcessor** - Default limit for batch jobs
3. **WaveCoordinator** - Fallback values for wave batch metadata
4. **BatchJobCompletedListener** - Progress calculations
5. **EnhancedImportStatusService** - Status reporting
6. **NetSuiteApiOptimizer** - API optimization settings

## Recommended Values

### Production Settings (Reliable)
```env
WAVE_BATCH_SIZE=100        # Prevents NetSuite empty responses
WAVE_MAX_BATCH_SIZE=100    # Hard limit for safety
WAVE_MIN_BATCH_SIZE=10     # Minimum efficient size
```

### Development Settings (Faster)
```env
WAVE_BATCH_SIZE=50         # Smaller for testing
WAVE_MAX_BATCH_SIZE=100    # Keep safety limit
WAVE_MIN_BATCH_SIZE=5      # Allow smaller test batches
```

### Testing Large Datasets
```env
WAVE_BATCH_SIZE=25         # Very small for problematic records
WAVE_MAX_BATCH_SIZE=50     # Conservative maximum
WAVE_MIN_BATCH_SIZE=1      # Allow single-record batches
```

## Trade-offs

### Smaller Batch Sizes (e.g., 100)
**Pros:**
- More reliable NetSuite API responses
- Faster individual requests
- Better error isolation
- Less memory usage per batch

**Cons:**
- More total batches (longer overall time)
- More database records in wave_batches
- More API calls (higher overhead)

### Larger Batch Sizes (e.g., 500)
**Pros:**
- Fewer total batches
- Less database overhead
- Fewer API calls

**Cons:**
- Higher chance of empty responses
- Longer individual request times
- More records affected by failures
- Higher memory usage

## Troubleshooting

### Empty Response Issues
- **Symptom**: NetSuite returns empty responses for SELECT queries
- **Solution**: Reduce `WAVE_BATCH_SIZE` to 100 or lower

### Too Many Batches
- **Symptom**: Extremely long import times
- **Solution**: Increase `WAVE_BATCH_SIZE` (but monitor for empty responses)

### Memory Issues
- **Symptom**: PHP memory limit exceeded
- **Solution**: Reduce `WAVE_BATCH_SIZE` to lower memory usage per batch

## Implementation Notes

All batch size references have been converted from hardcoded values to use `config('waves.batch_size', 100)` with appropriate fallbacks.

The configuration is loaded once per request and cached, so changes require a cache clear or application restart in production.
