# Environment-Aware Configuration Update

## Summary
Updated the ERP system to use environment-based configuration for localhost/production IP addresses, eliminating hardcoded localhost references in code.

## Changes Made

### 1. Config File (m1_erp_config/config.php)
**Updated HTTP_HOST detection** to use environment-based logic:
- **Development mode**: Uses `localhost:8080` as fallback
- **Production mode**: Uses `192.168.x.XX:8080` as fallback (replace `x.XX` with actual IP)
- Respects `$_SERVER['HTTP_HOST']` when available

```php
if (ENVIRONMENT === 'production') {
    $httpHost = $_SERVER['HTTP_HOST'] ?? '192.168.x.XX:8080';
} else {
    $httpHost = $_SERVER['HTTP_HOST'] ?? 'localhost:8080';
}
```

### 2. Core Classes Updated

#### core/AI.php
- **Ollama API endpoint** now uses environment-aware host
- Development: `http://localhost:11434/api`
- Production: `http://192.168.x.XX:11434/api` (configurable via `$_SERVER['OLLAMA_HOST']`)

#### core/NetworkMonitor.php
- No changes needed - `allowedHosts` array uses hostname matching, not URL building

#### core/RateLimit.php
- No changes needed - `trustedIps` array uses IP matching, not URL building

### 3. Controllers Updated

#### controllers/EmailController.php
- **SMTP host fallback** now environment-aware
- Development: `localhost`
- Production: `192.168.x.XX` (configurable via `$_SERVER['SMTP_HOST']`)

#### controllers/NextcloudController.php
- **Nextcloud/Collabora URL replacements** use environment-aware host
- Development: `http://localhost:11000`
- Production: `http://192.168.x.XX:11000` (configurable via `$_SERVER['NEXTCLOUD_HOST']`)
- Updated in 2 locations (createPublicShareLink and getCollaboraWopiUrl methods)

#### controllers/AITestController.php
- **API status endpoint** returns environment-aware Ollama API URL
- Matches the URL used by AI class

### 4. Views Updated

#### views/network_monitor/index.php
- Updated placeholder text to be generic ("local services" instead of "localhost:11434")

## How to Configure for Production

### Step 1: Update config.php
In `/Users/rpmbbu/LocalPHPStorm/m1_erp_config/config.php`, change line 13:
```php
define('ENVIRONMENT', 'production');
```

### Step 2: Replace IP Address Placeholders
Search and replace `192.168.x.XX` with your actual production IP address in:
- `m1_erp_config/config.php` (line 24)
- `core/AI.php` (line 19)
- `controllers/EmailController.php` (line 464)
- `controllers/NextcloudController.php` (lines 319, 385)
- `controllers/AITestController.php` (line 240)

### Step 3: Optional Environment Variables
You can also set environment variables to override defaults:
- `$_SERVER['OLLAMA_HOST']` - Ollama server host
- `$_SERVER['SMTP_HOST']` - SMTP server host
- `$_SERVER['NEXTCLOUD_HOST']` - Nextcloud server URL (with port)

## Files Not Modified

The following files were **intentionally not modified** because they are:
- Documentation files (docs/*)
- Test files (tests/, tests2/, public/test*.php, public/debug*.php)
- Backup files (backups/*)
- Vendor libraries (public/assets/plugins/*)
- Script files (scripts/*)
- Example files (dev_examples/*)
- Third-party code

These files contain localhost references for:
- Example URLs in documentation
- Test configurations
- Historical backups
- Third-party library code

## Testing

### Development Environment
No changes needed - everything should work as before with localhost.

### Production Environment
1. Change `ENVIRONMENT` to `'production'` in config.php
2. Replace all `192.168.x.XX` placeholders with actual production IP
3. Test each service:
   - Web access: `http://192.168.x.XX:8080`
   - Ollama AI: `http://192.168.x.XX:11434`
   - Nextcloud: `http://192.168.x.XX:11000`
   - SMTP: Check system_settings table or use environment variable

## Benefits

1. **Single Point of Configuration**: Change environment once in config.php
2. **No Code Changes Between Environments**: Same codebase works in dev and prod
3. **Flexibility**: Can override with environment variables if needed
4. **Maintainability**: Future developers know where to look for environment config
5. **Security**: Reduces risk of hardcoded production IPs in dev code

## Notes

- The `BASE_URL` constant is automatically set based on `HTTP_HOST`, so all `base_url()` helper calls will use the correct host
- Database host configuration is separate (DB_HOST constant) - not modified
- Session cookies and CSRF tokens will work correctly across environments
- All URL building throughout the app uses `base_url()` helper, which respects the environment

## Migration from Old System

Before this update, you had to manually find and replace localhost strings when deploying to production. Now:
1. Set `ENVIRONMENT` constant once
2. Replace `192.168.x.XX` placeholder once (or set env vars)
3. Deploy to any environment without code changes

## Future Enhancements

Consider creating environment-specific config files:
- `config.development.php`
- `config.production.php`
- Load based on `ENVIRONMENT` constant

This would eliminate the need to replace IP placeholders entirely.
