# Notifications

Turista sends reservation reminders through in-app database notifications and WhatsApp messages.

## ScheduledNotification model

`App\Models\ScheduledNotification` represents a reminder that should be sent at a specific time.

Key fields:

- `notifiable_type` / `notifiable_id` (morph)
- `reservation_id`
- `type` — e.g., `check_in`, `check_out`, `evacuate`
- `send_at`
- `queued_at`, `sent_at`, `cancelled_at`
- `payload` (array)

Scopes:

- `due` — notifications whose `send_at` has passed and are not yet sent/cancelled.
- `pendingForReservation` — unsent notifications for a given reservation.

## Reminder types

| Type | When it fires |
|------|---------------|
| `check_in` | Before the guest arrives. |
| `check_out` | Before the guest departs. |
| `evacuate` | When the stay should end. |

## Scheduler command

The `notifications:send-due` command runs every five minutes via `routes/console.php`:

```php
Schedule::command('notifications:send-due')->everyFiveMinutes()->withoutOverlapping();
```

It claims due notifications and dispatches `DispatchScheduledNotification` jobs.

## Dispatch job

`App\Jobs\DispatchScheduledNotification`:

1. Loads the scheduled notification and reservation.
2. Skips if the reservation is canceled or checked out.
3. Sends the `ReservationReminder` notification.
4. Marks the scheduled notification as sent.

## Notification channels

All WhatsApp notifications implement `Illuminate\Contracts\Queue\ShouldQueue` and are dispatched through the queue:

- `App\Notifications\PaymentProcessed`
- `App\Notifications\ReservationCreated`
- `App\Notifications\ReservationReminder`

Each notification routes through **`WhatsAppChannel`**, which sends the message via `WhatsAppService` / CoreVerde.

> **Note:** `App\Notifications\Channels\AppDatabaseChannel` exists but is not currently wired into any notification's `via()` array.

## User notifications

Authenticated users can list their notifications, mark them as read, and delete them.

## Cancellation

When a reservation is canceled or checked out, pending scheduled notifications for that reservation are canceled to avoid sending irrelevant reminders.
