# Notification System Implementation - Next Steps

## ✅ Completed

1. **Created Notification Classes:**
   - `app/Notifications/TaskAssigned.php` - Sent when a user is assigned to a task
   - `app/Notifications/TaskUpdated.php` - Sent when a task is updated by assignee

2. **Updated Livewire Component:**
   - Added notification imports to `app/Livewire/NewWeek.php`
   - Modified `assignUserToTask()` - Sends notifications to newly assigned users
   - Modified `markTaskAsCompleted()` - Notifies task creator when completed
   - Modified `markTaskAsCompletedWithAi()` - Notifies task creator when AI completes
   - Modified `addNote()` - Notifies task creator when note is added
   - Modified `updateTaskNote()` - Notifies task creator when note is updated
   - Modified `assignCompanyToTask()` - Notifies task creator when company is assigned
   - Modified `updateTaskColor()` - Notifies task creator when color is changed

3. **Email Configuration:**
   - SMTP configured with Yandex Mail in .env

## 📋 Required Next Steps

### 1. Create .env File and Configure Queue

Create a `.env` file (copy from `.env.example`) and add/update these settings:

```env
APP_NAME="Görev Planlayıcı"
APP_ENV=local
APP_KEY=your-app-key-here
APP_DEBUG=true
APP_URL=http://localhost

# Database Configuration
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password

# Queue Configuration
QUEUE_CONNECTION=database

# Mail Configuration (Already Set)
MAIL_MAILER=smtp
MAIL_HOST=smtp.yandex.com
MAIL_PORT=587
MAIL_USERNAME=noreply@apper.com.tr
MAIL_PASSWORD=cchwvkreuwlwyzbb
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=noreply@apper.com.tr
MAIL_FROM_NAME="Görev Bildirimi"
```

### 2. Run Migrations

After creating the .env file, run these commands:

```bash
# Generate application key
php artisan key:generate

# Run all migrations (includes notifications table and jobs table)
php artisan migrate
```

This will create:
- `notifications` table - Stores all notifications
- `jobs` table - Queue system for async email sending
- Other pending migrations

### 3. Start Queue Worker

For development, open a new terminal and run:

```bash
php artisan queue:work --tries=3 --timeout=90
```

Keep this running while testing the notification system.

For production, use Supervisor (see below).

### 4. Test the System

Test scenarios:

1. **Assignment Notification:**
   - Login as User A
   - Create a task
   - Assign it to User B
   - Check User B's email for notification

2. **Completion Notification:**
   - Login as User B (assignee)
   - Complete the task
   - Check User A's (creator) email for notification

3. **Note Addition:**
   - Login as User B
   - Add a note to User A's task
   - Check User A's email

4. **Check Database:**
   ```sql
   SELECT * FROM notifications ORDER BY created_at DESC LIMIT 10;
   ```

### 5. Production Setup (Optional)

For production, set up Supervisor to keep queue workers running:

**Install Supervisor:**
```bash
sudo apt-get install supervisor
```

**Create config file `/etc/supervisor/conf.d/laravel-worker.conf`:**
```ini
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/planner.apper/artisan queue:work database --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopwaitsecs=3600
numprocs=8
redirect_stderr=true
stdout_logfile=/path/to/planner.apper/storage/logs/worker.log
```

**Start Supervisor:**
```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
```

## 📧 Email Templates

The notification emails are sent in Turkish with the following structure:

### Task Assigned Email
- **Subject:** "Yeni Görev Atandı: {task note}"
- **Greeting:** "Merhaba {user name},"
- **Body:** "{assigned by} tarafından size yeni bir görev atandı."
- **Details:** Task note, company (if any), date (if any)
- **Action Button:** "Görevi Görüntüle" → Links to dashboard

### Task Updated Email
- **Subject:** "Görev Güncellendi: {task note}"
- **Greeting:** "Merhaba {user name},"
- **Body:** "{updated by} {action message}."
  - Actions: "görevi tamamladı", "göreve not ekledi", etc.
- **Details:** Task note, update details, company (if any)
- **Action Button:** "Görevi Görüntüle" → Links to dashboard

## 🔍 Troubleshooting

### Check if emails are being queued:
```bash
php artisan tinker
>>> App\Models\Task::first()->user->notifications
```

### Check queue status:
```bash
php artisan queue:failed
```

### Retry failed jobs:
```bash
php artisan queue:retry all
```

### Clear failed jobs:
```bash
php artisan queue:flush
```

### View logs:
```bash
tail -f storage/logs/laravel.log
```

### Test email configuration:
```bash
php artisan tinker
>>> Notification::route('mail', 'test@example.com')->notify(new \App\Notifications\TaskAssigned(\App\Models\Task::first(), 'Test'))
```

## 🎯 Summary

The notification system is now fully implemented and ready to use! 

**What happens now:**
1. When you assign a task → Assignee gets email + database notification
2. When assignee completes task → Creator gets email + database notification  
3. When assignee adds notes → Creator gets email + database notification
4. When assignee updates details → Creator gets email + database notification

All emails are queued for performance and won't slow down the UI.
