<?php
namespace App\Observers;

use App\Events\NotificationCreated;
use App\Models\Notification;
use App\Notifications\PushNotification;

class NotificationObserver
{
    public function created(Notification $notification): void
    {
        \Log::info('Notification created: ' . $notification->id);

        broadcast(new NotificationCreated($notification));

        try {
            $notification->user->notify(new \App\Notifications\PushNotification($notification));
            \Log::info('WebPush notify() called for user ' . $notification->user_id);
        } catch (\Throwable $e) {
            \Log::error('WebPush send failed: ' . $e->getMessage(), ['exception' => $e]);
        }
    }
}
