<?php
/**
 * Ragreen - Notification helper
 * Types in use: order_confirmed, order_sold, listing_hidden, account_reactivated,
 * listing_expiry, pantry_expiry. Free-text type field (no enum) so new event
 * types can be added anywhere without a schema migration.
 *
 * Also fires a push notification (see includes/fcm.php) to any devices the
 * user is registered on - this is the ONLY call site that does that, so
 * every existing and future caller of create_notification() gets push
 * "for free" without needing to know FCM exists. If FCM isn't configured,
 * that call is a silent no-op - this function's core job (writing the
 * in-app notification row) is completely unaffected either way.
 */

require_once __DIR__ . '/fcm.php';

function create_notification(PDO $pdo, int $userId, string $type, string $message): void
{
    $stmt = $pdo->prepare('INSERT INTO notifications (user_id, type, message) VALUES (?, ?, ?)');
    $stmt->execute([$userId, $type, $message]);

    fcm_notify_user($pdo, $userId, 'Ragreen', $message, ['type' => $type]);
}
