import type { NotificationEntity, NotificationCreationData, NotificationUpdateData } from '../entities/Notification';
import type { NotificationPort } from '../ports/NotificationPort';
import { NotificationError } from '../services/NotificationService';
import { httpClient } from '../../../lib/http-client';

/**
 * HTTP Adapter implementation for Notification domain
 */
export class NotificationAdapter implements NotificationPort {
  private readonly endpoint = '/notification';

  async getAll(): Promise<NotificationEntity[]> {
    try {
      const response = await httpClient.get<NotificationEntity[]>(this.endpoint);
      return response.data;
    } catch (error) {
      throw new NotificationError('REQUEST_ERROR', `Failed to fetch notification list`);
    }
  }

  async getById(id: string): Promise<NotificationEntity> {
    try {
      const response = await httpClient.get<NotificationEntity>(`${this.endpoint}/${id}`);
      return response.data;
    } catch (error) {
      throw new NotificationError('REQUEST_ERROR', `Failed to fetch notification with id ${id}`);
    }
  }

  async create(data: NotificationCreationData): Promise<NotificationEntity> {
    try {
      const response = await httpClient.post<NotificationEntity>(this.endpoint, data);
      return response.data;
    } catch (error) {
      throw new NotificationError('REQUEST_ERROR', `Failed to create notification`);
    }
  }

  async update(id: string, data: NotificationUpdateData): Promise<NotificationEntity> {
    try {
      const response = await httpClient.put<NotificationEntity>(`${this.endpoint}/${id}`, data);
      return response.data;
    } catch (error) {
      throw new NotificationError('REQUEST_ERROR', `Failed to update notification with id ${id}`);
    }
  }

  async delete(id: string): Promise<void> {
    try {
      await httpClient.delete(`${this.endpoint}/${id}`);
    } catch (error) {
      throw new NotificationError('REQUEST_ERROR', `Failed to delete notification with id ${id}`);
    }
  }
}