# Simplified Twilio SMS Service

## Overview
This is a simplified Twilio SMS integration that uses basic authentication (Account SID + Auth Token) instead of OAuth2. It includes automatic fake responses for local/testing environments.

## Configuration

### Admin Panel (Recommended)
Configure Twilio settings through the admin panel:
- Navigate to: `https://localhost:8000/admin/settings/integrations`
- Find "Twilio SMS Service (Recommended)" section
- Enter your credentials:
  - Account SID
  - Auth Token  
  - From Number (your Twilio phone number)

### Environment Variables (Fallback)
If not configured in admin panel, the service will use these env variables:
```env
TWILIO_SID=your_account_sid
TWILIO_TOKEN=your_auth_token
TWILIO_FROM=+441234567890
```

## Usage

### Basic SMS Sending
```php
use App\Modules\Twilio\Services\TwilioService;

$twilioService = app(TwilioService::class);

// Send simple SMS
$result = $twilioService->sendSMS('+447987654321', 'Hello from Twilio!');

if ($result['success']) {
    echo "SMS sent! SID: " . $result['message_sid'];
} else {
    echo "Failed: " . $result['error'];
}
```

### Task-Related SMS
```php
// Send SMS with task context (creates notification record)
$result = $twilioService->sendTaskSMS(
    $taskId,
    '+447987654321',
    'Your task has been updated'
);

// Send completion notification
$result = $twilioService->sendCompletionSMS(
    $taskId,
    '+447987654321',
    'Your task is complete!'
);
```

### Check Delivery Status
```php
$status = $twilioService->getDeliveryStatus($messageSid);
if ($status['success']) {
    echo "Status: " . $status['status'];
}
```

## Testing

### Automatic Fake Mode
In `local` and `testing` environments, the service automatically returns fake responses:
- No actual SMS is sent
- Returns fake message SID (e.g., `FAKE65abc123`)
- Logs the attempt for debugging
- Perfect for development without using credits

### Unit Testing Example
```php
public function test_sends_sms()
{
    $service = new TwilioService();
    
    // In testing environment, this returns a fake response
    $result = $service->sendSMS('+447987654321', 'Test message');
    
    $this->assertTrue($result['success']);
    $this->assertStringStartsWith('FAKE', $result['message_sid']);
}
```

## Phone Number Formatting
The service automatically formats UK phone numbers:
- `07987654321` → `+447987654321`
- `0798 765 4321` → `+447987654321`
- Already formatted numbers are preserved

## Key Features
✅ Simple basic authentication (no OAuth2 complexity)
✅ Automatic fake responses in local/testing environments
✅ Integration with admin settings panel
✅ Phone number formatting for UK numbers
✅ Task notification support with database tracking
✅ Clean error handling and logging

## Migration from Old Implementation
The old OAuth2-based implementation has been replaced. No migration needed - just ensure your credentials are set in the admin panel or environment variables.

## Troubleshooting

### SMS not sending in production
1. Check credentials in admin panel
2. Verify Twilio account is active and has credits
3. Check Laravel logs for error messages

### Getting "Twilio not configured" error
- Ensure credentials are set in admin panel or .env file
- Clear config cache: `php artisan config:clear`

### Phone number validation errors
- Ensure phone numbers include country code
- UK numbers starting with 0 are automatically formatted