/**
 * Services and business logic for the Notification domain.
 * @packageDocumentation
 */

import type {
  NotificationEntity,
  NotificationCreationData,
  NotificationUpdateData
} from '../entities/Notification';

import type {
  NotificationPort
} from '../ports/NotificationPort';

export class NotificationError extends Error {
  constructor(
    public code: string,
    message: string,
  ) {
    super(message);
    this.name = 'NotificationError';
  }
}

/**
 * Service implementation for Notification domain
 */
export class NotificationService {
  constructor(private readonly port: NotificationPort) {}

  /**
   * Get all notification entities
   */
  async getAll(): Promise<NotificationEntity[]> {
    try {
      return await this.port.getAll();
    } catch (error) {
      throw new NotificationError('GET_ALL_ERROR', 'Failed to get all notification entities');
    }
  }

  /**
   * Get a notification entity by ID
   */
  async getById(id: string): Promise<NotificationEntity> {
    try {
      return await this.port.getById(id);
    } catch (error) {
      throw new NotificationError('GET_BY_ID_ERROR', `Failed to get notification entity with id ${id}`);
    }
  }

  /**
   * Create a new notification entity
   */
  async create(data: NotificationCreationData): Promise<NotificationEntity> {
    try {
      return await this.port.create(data);
    } catch (error) {
      throw new NotificationError('CREATE_ERROR', 'Failed to create notification entity');
    }
  }

  /**
   * Update an existing notification entity
   */
  async update(id: string, data: NotificationUpdateData): Promise<NotificationEntity> {
    try {
      return await this.port.update(id, data);
    } catch (error) {
      throw new NotificationError('UPDATE_ERROR', `Failed to update notification entity with id ${id}`);
    }
  }

  /**
   * Delete a notification entity
   */
  async delete(id: string): Promise<void> {
    try {
      await this.port.delete(id);
    } catch (error) {
      throw new NotificationError('DELETE_ERROR', `Failed to delete notification entity with id ${id}`);
    }
  }
}
