const {
  getAllNotificationDb,
  getNotificationByIdDb,
  insertNotificationDb,
  updateNotificationDb,
  deleteNotificationDb,
  handleBrandCodeError // 🧩 tambahkan ini
} = require('../db/notification.db');

const { ErrorHandler } = require('../helpers/error');

class NotificationService {
  // Get all Notifications
  static async getAllNotification(param) {
    try {
      const results = await getAllNotificationDb(param);

      results.data.map(element => {
      });

      return results;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Get notification by ID
  static async getNotificationById(id) {
    try {
      const result = await getNotificationByIdDb(id);

      if (!result || (Array.isArray(result) && result.length < 1)) {
        throw new ErrorHandler(404, 'Notification not found');
      }

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  static async createNotification(data) {
    try {
      if (!data || typeof data !== 'object') data = {};

      const result = await insertNotificationDb(data);
      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  static async updateNotification(id, data) {
    try {
      if (!data || typeof data !== 'object') data = {};

      const dataExist = await getNotificationByIdDb(id);

      if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) {
        throw new ErrorHandler(404, 'Notification not found');
      }

      const result = await updateNotificationDb(id, data);
      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  static async deleteNotification(id, userId) {
    try {
      const dataExist = await getNotificationByIdDb(id);

      if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) {
        throw new ErrorHandler(404, 'Notification not found');
      }

      const result = await deleteNotificationDb(id, userId);
      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }
}

module.exports = NotificationService;
