# TeleHealth Hub Botswana - Setup & Installation Guide

## Quick Start

### Option 1: Using PHP Built-in Server (Development)

```bash
# Navigate to project directory
cd /home/ubuntu/telehealth-hub-php

# Start PHP server
php -S localhost:8000 -t public/

# Access in browser
# http://localhost:8000
```

### Option 2: Using Apache (Production)

1. Copy project to Apache web root:
```bash
sudo cp -r /home/ubuntu/telehealth-hub-php /var/www/html/telehealth
```

2. Configure Apache virtual host:
```apache
<VirtualHost *:80>
    ServerName telehealth.bw
    DocumentRoot /var/www/html/telehealth/public
    
    <Directory /var/www/html/telehealth/public>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
```

3. Enable the site:
```bash
sudo a2ensite telehealth
sudo systemctl restart apache2
```

## Database Setup

### Using MySQL Command Line

```bash
# Login to MySQL
mysql -u root -p

# Run initialization script
source /home/ubuntu/telehealth-hub-php/database/init.sql;

# Verify database created
SHOW DATABASES;
USE telehealth_hub_botswana;
SHOW TABLES;
```

### Using phpMyAdmin

1. Open phpMyAdmin
2. Create new database: `telehealth_hub_botswana`
3. Import `database/init.sql`
4. Create database user with appropriate permissions

## Configuration

### Step 1: Update Database Credentials

Edit `includes/config.php`:

```php
define('DB_HOST', 'localhost');
define('DB_USER', 'telehealth_user');
define('DB_PASS', 'your_secure_password');
define('DB_NAME', 'telehealth_hub_botswana');
```

### Step 2: Configure Orange Services

Update Orange API credentials in `includes/config.php`:

```php
// Orange SMS API
define('ORANGE_SMS_API_KEY', 'your_sms_api_key');
define('ORANGE_SMS_API_URL', 'https://api.orange.com/sms/send');

// Orange Money Web Payment
define('ORANGE_MONEY_API_KEY', 'your_money_api_key');
define('ORANGE_MONEY_API_URL', 'https://api.orange.com/payment');

// Orange USSD
define('ORANGE_USSD_CODE', '*123#');
define('ORANGE_USSD_CALLBACK_URL', 'https://yourdomain.com/ussd-handler.php');
```

### Step 3: Set File Permissions

```bash
# Make uploads directory writable
chmod -R 755 /home/ubuntu/telehealth-hub-php/public
chmod -R 755 /home/ubuntu/telehealth-hub-php/assets

# Make includes directory readable
chmod -R 755 /home/ubuntu/telehealth-hub-php/includes
```

## Testing

### Test User Accounts

| Role | Email | Password |
|------|-------|----------|
| Patient | patient@telehealth.bw | password123 |
| Doctor | dr.motswana@telehealth.bw | password123 |
| Nurse | nurse.keabetswe@telehealth.bw | password123 |
| Admin | admin@telehealth.bw | admin123 |

### Test USSD Flow

1. Navigate to landing page: http://localhost:8000
2. Scroll to "Interactive Prototype" section
3. Click "Start USSD Demo"
4. Enter phone number (optional)
5. Follow the interactive menu

### Test Consultations

1. Login as patient
2. Navigate to "Book Consultation"
3. Select a doctor
4. Choose consultation type and date
5. Submit

### Test Appointments

1. Login as patient
2. Navigate to "Clinics"
3. Select clinic
4. Choose available date/time
5. Confirm appointment

## API Endpoints

### USSD Handler
```
POST /ussd-handler.php

Request:
{
    "phoneNumber": "+267-71-000000",
    "sessionId": "unique-session-id",
    "userInput": "1",
    "sessionStatus": "NEW|CONTINUE|END"
}

Response:
{
    "sessionId": "unique-session-id",
    "phoneNumber": "+267-71-000000",
    "menuText": "Menu content...",
    "sessionStatus": "CONTINUE|END"
}
```

### SMS Notification Handler
```
POST /api/sms-handler.php

Request:
{
    "phone": "+267-71-000000",
    "message": "Your appointment is tomorrow at 09:00",
    "type": "appointment_reminder"
}

Response:
{
    "success": true,
    "message": "SMS sent successfully",
    "sms_id": "123456"
}
```

### Payment Handler
```
POST /api/payment-handler.php

Request:
{
    "amount": 100.00,
    "currency": "BWP",
    "phone": "+267-71-000000",
    "consultation_id": 1
}

Response:
{
    "success": true,
    "transaction_id": "TXN123456",
    "status": "completed"
}
```

## Troubleshooting

### Database Connection Error

**Problem:** "Connection failed: Connection refused"

**Solution:**
1. Verify MySQL is running: `sudo service mysql status`
2. Check database credentials in `config.php`
3. Ensure database user has proper permissions

### USSD Simulator Not Working

**Problem:** USSD demo doesn't respond to input

**Solution:**
1. Check browser console for JavaScript errors
2. Verify `assets/js/main.js` is loaded
3. Clear browser cache and reload

### Logo Not Displaying

**Problem:** Logo image shows as broken

**Solution:**
1. Verify logo file exists: `/home/ubuntu/telehealth-hub-php/assets/images/logo.png`
2. Check file permissions: `chmod 644 assets/images/logo.png`
3. Update image path in `public/index.php` if needed

### Session Issues

**Problem:** Login not working or sessions timing out

**Solution:**
1. Check PHP session settings in `php.ini`
2. Verify session directory is writable: `chmod 755 /var/lib/php/sessions`
3. Clear browser cookies and try again

## Performance Optimization

### Enable Caching

Add to `.htaccess`:
```apache
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
</IfModule>
```

### Enable Gzip Compression

Add to `.htaccess`:
```apache
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
```

### Database Optimization

```sql
-- Analyze tables
ANALYZE TABLE users;
ANALYZE TABLE consultations;
ANALYZE TABLE appointments;

-- Optimize tables
OPTIMIZE TABLE users;
OPTIMIZE TABLE consultations;
OPTIMIZE TABLE appointments;
```

## Security Hardening

### 1. Update Security Headers

Add to `.htaccess`:
```apache
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-XSS-Protection "1; mode=block"
Header set Referrer-Policy "strict-origin-when-cross-origin"
```

### 2. Enable HTTPS

```bash
# Install SSL certificate
sudo certbot certonly --apache -d yourdomain.com

# Update .htaccess to redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
```

### 3. Secure Cookies

Update `includes/config.php`:
```php
session_set_cookie_params([
    'secure' => true,      // HTTPS only
    'httponly' => true,    // No JavaScript access
    'samesite' => 'Strict' // CSRF protection
]);
```

### 4. Rate Limiting

Implement in `.htaccess`:
```apache
<IfModule mod_ratelimit.c>
    SetOutputFilter RATE_LIMIT
    LimitRequestBody 512000
    LimitRequestFields 20
    LimitRequestFieldSize 8190
</IfModule>
```

## Deployment Checklist

- [ ] Database configured and tested
- [ ] Orange API credentials configured
- [ ] SSL/HTTPS enabled
- [ ] Email service configured
- [ ] SMS gateway tested
- [ ] Payment gateway tested
- [ ] File permissions set correctly
- [ ] Security headers configured
- [ ] Backup strategy implemented
- [ ] Monitoring and logging enabled
- [ ] Performance optimization applied
- [ ] Load testing completed
- [ ] Security audit passed

## Support

For issues or questions:
- Email: support@telehealth.bw
- Phone: +267-3-123456
- Website: www.telehealth.bw

## Additional Resources

- [PHP Documentation](https://www.php.net/docs.php)
- [MySQL Documentation](https://dev.mysql.com/doc/)
- [Orange API Documentation](https://developer.orange.com/)
- [Web Security Best Practices](https://owasp.org/)

---

**Last Updated:** May 2024  
**Version:** 1.0.0
