# SMS Debugging Improvements

## What Was Added

### 1. Enhanced Logging in SmsService.php

Added detailed logging at every step of the SMS sending process:

- **Before sending**: Logs provider, phone number, sender ID, message length
- **API request**: Logs URL, headers (without exposing API key)
- **API response**: Logs status code and full response
- **Success**: Logs successful delivery
- **Errors**: Logs detailed error information with stack trace

### 2. Test SMS Feature in Admin Settings

Added a "Test SMS Configuration" button in Admin Settings:

**Location**: Admin Dashboard → Settings → SMS Configuration

**Features**:
- One-click test SMS sending
- Uses admin alert phone number
- Shows real-time success/failure feedback
- Helps verify configuration before going live

**How to Use**:
1. Enter your phone number in "Admin Alert Phone Number"
2. Click "Send Test SMS" button
3. Check your phone for test message
4. See success/failure message on screen

### 3. Improved Error Messages

Enhanced error messages to be more descriptive:

- Shows which provider failed (SMS Online GH or BulkSMS Ghana)
- Includes API response details
- Shows phone number that failed (for debugging)
- Includes HTTP status codes

### 4. Comprehensive Troubleshooting Guide

Created `SMS_TROUBLESHOOTING.md` with:
- Quick checklist for common issues
- Step-by-step debugging instructions
- Common error messages and solutions
- Code examples for testing
- Log locations and what to look for

## How to Debug SMS Issues

### Step 1: Use Test SMS Button
1. Go to Admin Settings
2. Scroll to SMS Configuration
3. Enter your phone number
4. Click "Send Test SMS"
5. Check if you receive the message

### Step 2: Check Laravel Logs
```bash
# View recent SMS logs
tail -100 storage/logs/laravel.log | grep -i sms

# Watch logs in real-time
tail -f storage/logs/laravel.log | grep -i sms
```

### Step 3: Look for These Log Entries

**Successful SMS**:
```
[timestamp] local.INFO: Attempting to send SMS {"provider":"smsonlinegh","phone":"+233XXXXXXXXX",...}
[timestamp] local.INFO: SMS Online GH - Sending request {...}
[timestamp] local.INFO: SMS Online GH - Response received {"status_code":200,...}
[timestamp] local.INFO: SMS Online GH - Success {...}
```

**Failed SMS**:
```
[timestamp] local.ERROR: SMS API key not configured {"provider":"smsonlinegh"}
[timestamp] local.ERROR: SMS Online GH API error {"status_code":401,...}
[timestamp] local.ERROR: SMS exception {"error":"...","provider":"smsonlinegh",...}
```

### Step 4: Verify Configuration

Check these settings in Admin Dashboard:
- SMS Provider: Should be "SMS Online GH"
- SMS API Key: Should not be empty
- Sender ID: Should be approved by provider
- Admin Alert Phone: Should be in correct format

## Common Issues and Quick Fixes

### Issue 1: No SMS Received
**Check**: 
- Is SMS provider set to "SMS Online GH"?
- Is API key configured?
- Does customer have phone number?

**Fix**: Use Test SMS button to verify configuration

### Issue 2: Wrong Provider Selected
**Check**: SMS Provider dropdown in settings
**Fix**: Change to "SMS Online GH" and save

### Issue 3: Invalid API Key
**Check**: Logs show "401 Unauthorized"
**Fix**: 
- Verify API key is correct
- Check for extra spaces
- Regenerate key if needed

### Issue 4: Phone Number Format
**Check**: Logs show "Invalid recipient"
**Fix**: 
- Phone should be +233XXXXXXXXX or 0XXXXXXXXX
- System auto-converts, but verify in logs

### Issue 5: Insufficient Credits
**Check**: Logs show "Insufficient balance"
**Fix**: Top up SMS credits in provider dashboard

## Files Modified

1. **app/Services/SmsService.php**
   - Added detailed logging throughout
   - Enhanced error messages
   - Better exception handling

2. **resources/views/admin/settings.blade.php**
   - Added Test SMS button
   - Added JavaScript for test functionality
   - Added result display area

3. **app/Http/Controllers/Admin/AdminController.php**
   - Added `testSms()` method
   - Handles test SMS requests

4. **routes/web.php**
   - Added `/admin/test-sms` route

## Testing Your Configuration

### Method 1: Test SMS Button (Recommended)
1. Go to Admin Settings
2. Click "Send Test SMS"
3. Check your phone

### Method 2: Make a Test Withdrawal
1. Create a test user with phone number
2. Add balance to test user
3. Make a withdrawal request
4. Check if SMS is received

### Method 3: Check Logs
1. Make any SMS-triggering action
2. Check `storage/logs/laravel.log`
3. Look for SMS-related entries

## What to Check When SMS Fails

1. **Settings**
   - [ ] SMS provider is "SMS Online GH"
   - [ ] API key is entered
   - [ ] Sender ID is set
   - [ ] Admin phone is set

2. **User Data**
   - [ ] Customer has phone number
   - [ ] Phone format is correct
   - [ ] Phone is not blocked

3. **Provider Account**
   - [ ] SMS credits available
   - [ ] Account is active
   - [ ] Sender ID is approved

4. **System**
   - [ ] No errors in logs
   - [ ] Test SMS works
   - [ ] Internet connection OK

## Next Steps

1. **Test your configuration** using the Test SMS button
2. **Check logs** for any errors
3. **Verify settings** are correct
4. **Contact provider** if issues persist

## Support Resources

- **SMS Online GH**: https://smsonlinegh.com
- **Laravel Logs**: `storage/logs/laravel.log`
- **Troubleshooting Guide**: `SMS_TROUBLESHOOTING.md`
