import type { NotificationGatewayRegistry } from '@core/notifications/application/ports/NotificationGateway';
import type { NotificationService, SendNotificationCommand } from '@core/notifications/application/ports/NotificationService';
import { SendNotificationUseCase } from '@core/notifications/application/use-cases/SendNotificationUseCase';
import type { NotificationRepository } from '@core/notifications/domain/repositories/NotificationRepository';
import type { NotificationPreferenceRepository } from '@core/notifications/domain/repositories/NotificationPreferenceRepository';
import type { Logger } from '@core/shared/domain/Logger';

export class NotificationServiceAdapter implements NotificationService {
  private readonly useCase: SendNotificationUseCase;
  private readonly logger: Logger;

  constructor(
    notificationRepository: NotificationRepository,
    preferenceRepository: NotificationPreferenceRepository,
    gatewayRegistry: NotificationGatewayRegistry,
    logger: Logger,
  ) {
    this.logger = logger;
    this.useCase = new SendNotificationUseCase(
      notificationRepository,
      preferenceRepository,
      gatewayRegistry,
      logger,
    );
  }

  async sendNotification(command: SendNotificationCommand): Promise<void> {
    const result = await this.useCase.execute(command);
    
    if (result.isErr()) {
      const error = result.error;
      if (error) {
        this.logger.error('Failed to send notification', new Error(error.details.message));
        throw new Error(error.details.message);
      } else {
        throw new Error('Unknown error sending notification');
      }
    }
  }
}
