# SMS Notification Setup Guide

This guide will help you configure SMS notifications for the attendance system.

## Features

- **Absent Student Notifications**: Parents receive SMS when their child is marked absent
- **Event Announcements**: All parents receive SMS when new events are created

## Prerequisites

1. An SMS service provider account (Twilio, Vonage/Nexmo, or custom API)
2. Phone numbers for parents/guardians stored in the database

## Step 1: Database Setup

The system uses the existing `phone` field in the students table for parent phone numbers. 
If the `phone` field doesn't exist, add it:

```sql
-- Run this in phpMyAdmin or MySQL command line (only if phone field doesn't exist)
ALTER TABLE students 
ADD COLUMN phone VARCHAR(20) NULL;
```

## Step 2: Update Student Records

Add parent phone numbers to student records:

```sql
UPDATE students 
SET phone = '+1234567890' 
WHERE student_id = 'STUDENT001';
```

**Phone Number Format**: 
- For Semaphore (Philippines): Can use local format (09171234567) or international format (+639171234567)
- For other providers: Use international format with country code (e.g., +1234567890 for US/Canada)

**Note**: The `phone` field stores the parent/guardian phone number.

## Step 3: Configure SMS Provider

Edit `config.php` and configure your SMS provider:

### Option A: Semaphore (Currently Configured) ✅

```php
define('SMS_ENABLED', true);
define('SMS_PROVIDER', 'semaphore');
define('SMS_API_KEY', 'caafa1c8cf4987d72d0d35bf10d96a2d'); // Your Semaphore API key
define('SMS_COUNTRY_CODE', '+63'); // +63 for Philippines
```

**Note**: Semaphore is already configured in your system! Phone numbers can be in local format (09171234567) or international format (+639171234567).

### Option B: Twilio

```php
define('SMS_ENABLED', true);
define('SMS_PROVIDER', 'twilio');
define('SMS_API_KEY', 'your_twilio_account_sid');
define('SMS_API_SECRET', 'your_twilio_auth_token');
define('SMS_FROM_NUMBER', '+1234567890'); // Your Twilio phone number
define('SMS_COUNTRY_CODE', '+1'); // Default country code
```

### Option C: Vonage/Nexmo

```php
define('SMS_ENABLED', true);
define('SMS_PROVIDER', 'nexmo');
define('SMS_API_KEY', 'your_vonage_api_key');
define('SMS_API_SECRET', 'your_vonage_api_secret');
define('SMS_FROM_NUMBER', 'YourSenderID'); // Sender ID or number
define('SMS_COUNTRY_CODE', '+1');
```

### Option D: Custom SMS API

```php
define('SMS_ENABLED', true);
define('SMS_PROVIDER', 'custom');
define('SMS_API_KEY', 'your_api_key');
define('SMS_CUSTOM_URL', 'https://your-sms-api.com/send');
define('SMS_COUNTRY_CODE', '+1');
```

## Step 4: Test SMS Notifications

1. **Test Absent Notifications**:
   - Mark a student as absent after 4 PM
   - Check if parent receives SMS

2. **Test Event Notifications**:
   - Create a new event
   - Check if all parents receive SMS

## Troubleshooting

### SMS Not Sending

1. Check `config.php` - ensure `SMS_ENABLED` is set to `true`
2. Verify API credentials are correct
3. Check PHP error logs for SMS errors
4. Ensure phone numbers are in correct format (international format with +)

### Phone Number Format Issues

- Use international format: `+1234567890`
- Remove spaces, dashes, and parentheses
- Include country code

### Rate Limiting

The system includes a 0.1 second delay between SMS sends to avoid rate limiting. For large batches, consider:
- Using a queue system
- Sending in smaller batches
- Upgrading your SMS provider plan

## Disabling SMS

To disable SMS notifications without removing code:

```php
define('SMS_ENABLED', false);
```

## Cost Considerations

- **Semaphore**: ~₱0.50-1.00 per SMS (Philippines) - Very affordable for local use
- Twilio: ~$0.0075 per SMS (US)
- Vonage: ~$0.0059 per SMS (US)
- Check your provider's pricing for international numbers

**Note**: Semaphore is ideal for Philippine phone numbers and offers competitive rates.

## Support

For issues or questions, check:
- SMS provider documentation
- PHP error logs
- Database phone number format

