import { adapters, newUuid, migrationFields } from '@/services/serviceConfig';
import { auditService } from '@/services/auditService';

// In-app notification store. Email delivery is NOT configured during hosted phase —
// callers must not claim an email was sent. This service records in-app notifications only.
const ENTITY = 'Notification';

export const notificationService = {
  list: () => adapters.database.list(ENTITY),
  listForUser: (userId) => adapters.database.filter(ENTITY, { target_user_id: userId }),
  create: ({ target_user_id, target_email, type, title, body, link, related_uuid }, actor = 'Workshop Owner') =>
    adapters.database.create(ENTITY, {
      uuid: newUuid('notif'), target_user_id, target_email, type, title, body, link, related_uuid,
      read: false, created_date: new Date().toISOString(),
      ...migrationFields(actor),
    }),

  // Helpers for common events. Returns { emailDelivered: false } so the UI can show the honest message.
  notify: async (opts) => {
    const rec = await notificationService.create(opts);
    return { notification: rec, emailDelivered: false };
  },

  markRead: (n) => adapters.database.update(ENTITY, n.id, { read: true }),
};