#!/bin/bash
# Usage: ./test-notif.sh <email> [priority]
cd /var/www/html/dev/app

EMAIL=${1}
PRIORITY=${2:-medium}

if [ -z "$EMAIL" ]; then
    echo "Usage: $0 <email> [critical|high|medium|low]"
    exit 1
fi

php artisan tinker --execute="
use App\Models\\Notification;
use App\Models\\NotificationType;
use App\Models\\User;
use App\Services\\Api\\V1\\Fleet\\Notification\\NotificationBroadcastService;
use Illuminate\\Support\\Str;

\$user = User::where('email', '$EMAIL')->first();
if (!\$user) { echo 'User not found: $EMAIL'; exit; }

// Get the company id — fallback for owners whose user_id may be null
\$company = \$user->userCompany()->first();
\$companyId = \$company ? \$company->id : (\$user->user_id ?? \$user->id);

\$type = NotificationType::where('slug', 'speeding')->first() ?? NotificationType::first();

\$n = Notification::create([
    'id' => Str::uuid()->toString(),
    'type' => 'App\\\\Notifications\\\\Test',
    'notifiable_type' => 'App\\\\Models\\\\User',
    'notifiable_id' => \$user->id,
    'user_id' => \$user->id,
    'company_id' => \$companyId,
    'notification_type_id' => \$type->id,
    'title' => 'Test: Hmimi priority',
    'summary' => 'Test notification at ' . now()->format('H:i:s'),
    'consequences' => 'Test consequences',
    'priority' => '$PRIORITY',
    'notification_status' => 'unread',
    'data' => '{}',
]);

app(NotificationBroadcastService::class)->broadcastNewNotification(\$n);
app(NotificationBroadcastService::class)->broadcastCountUpdate(\$user->id, (int)\$companyId);

echo \"Sent {$PRIORITY} notification {\$n->id} to {\$user->email} (user_id: {\$user->id}, company_id: {\$companyId})\";
"

