<?php

namespace App\Traits;

use App\Models\Client;

trait ChecksNotificationPreference
{
    /**
     * Check if a notification should be sent based on client group preferences.
     *
     * Returns true if:
     * - Client has no group
     * - Group has no notification_settings
     * - The specific event type is not configured (null = send by default)
     * - The event type is explicitly enabled (true)
     *
     * Returns false only if the event type is explicitly disabled (false/0).
     */
    protected function shouldSendNotification(?Client $client, string $eventType): bool
    {
        if (!$client || !$client->group_id) {
            return true;
        }

        $group = $client->clientGroup ?? $client->group;
        if (!$group) {
            return true;
        }

        $preference = $group->getNotificationPreference($eventType);

        // null = not configured = send (default behavior)
        if ($preference === null) {
            return true;
        }

        return (bool) $preference;
    }
}
