# Email Testing Guide

This guide explains how to test all transactional emails in the system using the `email:send-test` artisan command.

## Quick Start

### List All Available Emails

```bash
php artisan email:send-test --list
```

### Send a Specific Email

```bash
php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102
```

### Interactive Mode

Run without options for an interactive menu:

```bash
php artisan email:send-test
```

## Available Email Types

### Inactive Profile Warnings

Test the automated profile deletion warning emails:

```bash
# First warning (2 weeks remaining)
php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102

# Second warning (1 week remaining)
php artisan email:send-test --type=inactive-warning-2 --receiver=user --id=102

# Final warning (24 hours remaining)
php artisan email:send-test --type=inactive-warning-final --receiver=user --id=102
```

**Supports:** user, organization

### Account Management

```bash
# User account deleted notification
php artisan email:send-test --type=user-deleted --receiver=user --id=102

# Email verification request
php artisan email:send-test --type=verify-email --receiver=user --id=102

# Profile name/link changed notification
php artisan email:send-test --type=profile-link-changed --receiver=user --id=102

# Profile edited by admin notification
php artisan email:send-test --type=profile-edited-by-admin --receiver=user --id=102
```

### Transactions

```bash
# Transfer/payment received notification
php artisan email:send-test --type=transfer-received --receiver=user --id=102
```

**Supports:** user, organization

### Reservations

```bash
# Reservation created
php artisan email:send-test --type=reservation-created --receiver=user --id=102

# Reservation cancelled
php artisan email:send-test --type=reservation-cancelled --receiver=user --id=102

# Reservation updated
php artisan email:send-test --type=reservation-updated --receiver=user --id=102
```

**Supports:** user, organization

### Social Interactions

```bash
# New comment/reaction on post
php artisan email:send-test --type=reaction-created --receiver=user --id=102

# Tag added to profile
php artisan email:send-test --type=tag-added --receiver=user --id=102
```

**Supports:** user, organization

## Command Options

### --type
Specifies which email type to send. Use `--list` to see all available types.

### --receiver
Specifies the receiver profile type:
- `user` - Individual user profiles
- `organization` - Organization profiles
- `admin` - Admin profiles
- `bank` - Bank profiles

### --id
The ID of the receiver profile.

### --queue
Send emails via queue instead of immediately:

```bash
php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102 --queue
```

Then process the queue:

```bash
php artisan queue:work --stop-when-empty
```

### --list
Display all available email types with descriptions:

```bash
php artisan email:send-test --list
```

## Usage Examples

### Test All Inactive Profile Warnings

```bash
# Send all 3 warning levels
php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102
php artisan email:send-test --type=inactive-warning-2 --receiver=user --id=102
php artisan email:send-test --type=inactive-warning-final --receiver=user --id=102
```

### Test Organization Emails

```bash
# Get an organization ID first
php artisan tinker --execute="echo App\Models\Organization::first()->id;"

# Then Send test mailing email
php artisan email:send-test --type=transfer-received --receiver=organization --id=1
```

### Test with Queue

```bash
# Queue the emails
php artisan email:send-test --type=user-deleted --receiver=user --id=102 --queue
php artisan email:send-test --type=transfer-received --receiver=user --id=102 --queue

# Process all queued emails
php artisan queue:work --stop-when-empty
```

## Finding Profile IDs

### Find User ID by Email

```bash
php artisan tinker --execute="echo App\Models\User::where('email', 'user@example.com')->first()->id;"
```

### List Recent Users

```bash
php artisan tinker --execute="App\Models\User::latest()->take(5)->get(['id', 'name', 'email'])->each(function(\$u) { echo \$u->id . ' - ' . \$u->name . ' (' . \$u->email . ')' . PHP_EOL; });"
```

### List Organizations

```bash
php artisan tinker --execute="App\Models\Organization::latest()->take(5)->get(['id', 'name', 'email'])->each(function(\$o) { echo \$o->id . ' - ' . \$o->name . ' (' . \$o->email . ')' . PHP_EOL; });"
```

## Test Data

The command automatically generates realistic test data for each email type:

- **Account balances**: Uses actual account data from the receiver's profile
- **Time remaining**: Realistic values (2 weeks, 1 week, 24 hours)
- **Transaction amounts**: Sample amounts formatted correctly
- **Dates**: Future dates for reservations
- **Names/URLs**: Generated test data with proper formatting

## Troubleshooting

### Email Not Received

1. Check if the email was sent successfully (command should show ✅)
2. Verify the email address is correct
3. Check spam/junk folders
4. Verify mail configuration in `.env`

### Receiver Not Found Error

```
Receiver not found: user #102
```

Solution: Verify the receiver exists and the ID is correct:

```bash
php artisan tinker --execute="echo App\Models\User::find(102) ? 'Found' : 'Not found';"
```

### Invalid Email Type Error

```
Invalid email type: xyz
```

Solution: Use `--list` to see all available types:

```bash
php artisan email:send-test --list
```

### Unsupported Receiver Type

```
Email type 'user-deleted' does not support receiver type 'organization'
```

Solution: Check which receiver types are supported for that email type using `--list`.

## Theme Testing

Emails use theme-aware colors. Test across all themes by changing `TIMEBANK_THEME` in `.env`:

```bash
# Test with uuro theme
TIMEBANK_THEME=uuro php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102

# Test with vegetable theme
TIMEBANK_THEME=vegetable php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102

# Test with yellow theme
TIMEBANK_THEME=yellow php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102

# Test with default theme
TIMEBANK_THEME=timebank_cc php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102
```

## Language Testing

Emails are sent in the receiver's preferred language (`lang_preference`). To test different languages:

1. Change a user's language preference:
```bash
php artisan tinker --execute="\$u = App\Models\User::find(102); \$u->lang_preference = 'nl'; \$u->save(); echo 'Updated';"
```

2. Send test mailing email:
```bash
php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=102
```

3. Repeat for other languages: `en`, `nl`, `de`, `es`, `fr`

## Batch Testing Script

Create a bash script to test all emails for a single user:

```bash
#!/bin/bash
USER_ID=102

echo "Testing all transactional emails for user #$USER_ID"

php artisan email:send-test --type=inactive-warning-1 --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=inactive-warning-2 --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=inactive-warning-final --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=user-deleted --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=transfer-received --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=profile-link-changed --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=profile-edited-by-admin --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=verify-email --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=reservation-created --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=reservation-cancelled --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=reservation-updated --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=reaction-created --receiver=user --id=$USER_ID --queue
php artisan email:send-test --type=tag-added --receiver=user --id=$USER_ID --queue

echo "All emails queued. Processing queue..."
php artisan queue:work --stop-when-empty

echo "✅ All test emails sent!"
```

Save as `test-all-emails.sh`, make executable, and run:

```bash
chmod +x test-all-emails.sh
./test-all-emails.sh
```
