# Chat Auto-Delete Feature Setup

## Overview
This feature automatically deletes chat messages older than a specified number of days (default: 1 day) to maintain data privacy and optimize storage.

## How It Works
- **Command**: `php artisan chat:delete-old-messages`
- **Scheduler**: Runs daily at midnight via Laravel scheduler
- **Configuration**: Uses `chat_message_auto_delete_days` setting (defaults to 1 day)
- **Scope**: Deletes all chat messages older than the configured days

## Setup Instructions

### 1. Cron Job Setup
Add the following cron job to your server to run the Laravel scheduler every minute:

```bash
* * * * * cd /path/to/your/project && php artisan schedule:run >> /dev/null 2>&1
```

### 2. Configuration
The auto-delete days can be configured in two ways:

#### Option A: Via Settings API
```bash
# Update to 7 days
curl -X PUT "http://your-domain.com/api/settings/chat_message_auto_delete_days" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"value": "7"}'
```

#### Option B: Via Database
```sql
UPDATE settings SET value = '7' WHERE key = 'chat_message_auto_delete_days';
```

#### Option C: Manual Command Override
```bash
# Delete messages older than 3 days
php artisan chat:delete-old-messages 3
```

## Manual Testing

### Test the Command
```bash
# Test with default setting
php artisan chat:delete-old-messages

# Test with custom days
php artisan chat:delete-old-messages 7

# View scheduled tasks
php artisan schedule:list
```

### Test with Different Timeframes
```bash
# Delete messages older than 1 hour (for testing)
php artisan chat:delete-old-messages 0.04

# Delete messages older than 30 days
php artisan chat:delete-old-messages 30
```

## Monitoring

### Check Scheduler Status
```bash
# View all scheduled tasks
php artisan schedule:list

# Run scheduler manually
php artisan schedule:run
```

### Log Files
Monitor your Laravel log file for deletion activities:
```bash
tail -f storage/logs/laravel.log | grep "chat"
```

## Security Considerations
- Only staff users can manually run the command
- Automatic deletion runs via system cron job
- Messages are permanently deleted (not soft-deleted)
- Consider backing up important conversations before enabling

## Customization

### Modify Schedule Frequency
Edit `routes/console.php`:
```php
// Run every 6 hours instead of daily
Schedule::command('chat:delete-old-messages')->everySixHours();

// Run weekly on Sundays at 2 AM
Schedule::command('chat:delete-old-messages')->weekly()->sundays()->at('02:00');
```

### Add Notification
Modify the command to send notifications when messages are deleted:
```php
// In DeleteOldChatMessages.php handle() method
if ($deletedCount > 0) {
    // Send email or notification to admin
    $this->info("Sent notification for {$deletedCount} deleted messages.");
}
```

## Troubleshooting

### Common Issues
1. **Cron job not running**: Verify cron syntax and paths
2. **Permission issues**: Ensure web server can write to storage/logs
3. **Messages not deleting**: Check the setting value and message timestamps

### Debug Commands
```bash
# Check current setting
php artisan tinker
>>> Setting::where('key', 'chat_message_auto_delete_days')->first()

# Count old messages
php artisan tinker
>>> Message::where('created_at', '<', now()->subDays(1))->count()

# Test deletion without actually deleting
php artisan tinker
>>> Message::where('created_at', '<', now()->subDays(1))->get()
```

## Default Settings
- **Auto-delete days**: 1 day
- **Schedule**: Daily at midnight (00:00)
- **Scope**: All chat messages in the system
- **Backup**: No automatic backup (configure separately if needed)

## Notes
- The feature is now active and will run automatically once the cron job is set up
- Existing messages will be deleted based on their creation timestamp
- The setting can be changed at any time and will take effect on the next run
- Consider your data retention policies before enabling automatic deletion
