<?php

namespace App\Modules\HR\Offboarding\Listeners;

use App\Models\User;
use App\Modules\HR\Offboarding\Events\OffboardingSubmitted;
use App\Modules\HR\Offboarding\Notifications\OffboardingSubmittedNotification;
use Illuminate\Support\Facades\Notification;

class OffboardingNotificationListener
{
    public function handleSubmitted(OffboardingSubmitted $event): void
    {
        $reviewers = User::query()
            ->where('tenant_id', $event->case->tenant_id)
            ->where('is_active', true)
            ->get()
            ->filter(fn (User $user): bool => $user->can('offboarding.manage'));

        if ($reviewers->isNotEmpty()) {
            Notification::send($reviewers, new OffboardingSubmittedNotification($event->case));
        }
    }
}
