import { createNotification } from './notificationService';

/**
 * Utility for dispatching notifications to multiple users.
 * 
 * @param {string[]} userIds - Array of UUIDs for users to notify
 * @param {Object} notificationData - Object containing type, title, message, relatedItemId, relatedItemType
 * @returns {Promise<Object>} - Summary of success/failure
 */
export async function dispatchBatchNotifications(userIds, notificationData) {
  if (!Array.isArray(userIds) || userIds.length === 0) {
    return { success: false, error: 'No recipients provided' };
  }

  const results = {
    total: userIds.length,
    success: 0,
    failed: 0,
    errors: []
  };

  try {
    const promises = userIds.map(userId => 
      createNotification({
        userId,
        ...notificationData
      }).then(res => {
        if (res.success) {
          results.success++;
        } else {
          results.failed++;
          results.errors.push({ userId, error: res.error });
        }
      })
    );

    await Promise.all(promises);
    return results;

  } catch (error) {
    console.error('Batch notification dispatch error:', error);
    return { success: false, error: error.message };
  }
}

/**
 * Helper to notify a list of users about a new comment.
 */
export async function notifyUsersOfComment(userIds, itemId, itemType, commenterName) {
    return dispatchBatchNotifications(userIds, {
        type: 'comment',
        title: 'New Comment',
        message: `${commenterName} commented on a ${itemType}.`,
        relatedItemId: itemId,
        relatedItemType: itemType
    });
}