<?php
//includes/internal_notification_helper.php'
/**
 * Add a notification for a member
 */
function addInternalNotification(PDO $pdo, int $memberId, string $title, string $message, string $type = 'info', ?string $actionUrl = null, string $icon = 'carbon:notification'): bool {
    $stmt = $pdo->prepare("
        INSERT INTO member_internal_notifications 
        (member_id, title, message, type, action_url, icon, status, created_at)
        VALUES (?, ?, ?, ?, ?, ?, 'unread', NOW())
    ");
    return $stmt->execute([$memberId, $title, $message, $type, $actionUrl, $icon]);
}

/**
 * Mark a single notification as read
 */
function markNotificationAsRead(int $notificationId, int $memberId, PDO $pdo): void {
    $stmt = $pdo->prepare("
        UPDATE member_internal_notifications
        SET status = 'read', read_at = NOW()
        WHERE id = ? AND member_id = ?
    ");
    $stmt->execute([$notificationId, $memberId]);
}

/**
 * Mark all notifications as read for a member
 */
function markAllNotificationsAsRead(int $memberId, PDO $pdo): int {
    $stmt = $pdo->prepare("
        UPDATE member_internal_notifications
        SET status = 'read', read_at = NOW()
        WHERE member_id = ? AND status = 'unread'
    ");
    $stmt->execute([$memberId]);
    return $stmt->rowCount();
}

/**
 * Fetch all notifications for a member
 */
function getAllMemberNotifications(int $memberId, PDO $pdo): array {
    $stmt = $pdo->prepare("
        SELECT * FROM member_internal_notifications
        WHERE member_id = ?
        ORDER BY created_at DESC
    ");
    $stmt->execute([$memberId]);
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

// Add this to internal_notification_helper.php
/**
 * Alias function for backward compatibility
 */
function sendNotification($userId, $title, $message, $pdo, $link = null, $senderId = null, $senderType = null, $icon = null, $type = 'info', $status = null, $userType = null) {
    // Map parameters to addInternalNotification
    // Note: Some parameters are ignored as they don't exist in addInternalNotification
    return addInternalNotification($pdo, $userId, $title, $message, $type, $link, $icon);
}