/**
 * Application Use Case: GetUnreadNotificationsUseCase
 * 
 * Retrieves unread notifications for a recipient.
 */

import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import type { Notification } from '../../domain/entities/Notification';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';

export interface UnreadNotificationsResult {
  notifications: Notification[];
  totalCount: number;
}

export class GetUnreadNotificationsUseCase implements AsyncUseCase<string, UnreadNotificationsResult> {
  constructor(
    private readonly notificationRepository: INotificationRepository,
    private readonly logger: Logger,
  ) {}

  async execute(recipientId: string): Promise<UnreadNotificationsResult> {
    this.logger.debug(`Attempting to retrieve unread notifications for recipient ID: ${recipientId}`);
    try {
      const notifications = await this.notificationRepository.findUnreadByRecipientId(recipientId);
      this.logger.info(`Successfully retrieved ${notifications.length} unread notifications for recipient ID: ${recipientId}`);
      
      if (notifications.length === 0) {
        this.logger.warn(`No unread notifications found for recipient ID: ${recipientId}`);
      }

      return {
        notifications,
        totalCount: notifications.length,
      };
    } catch (error) {
      this.logger.error(`Failed to retrieve unread notifications for recipient ID: ${recipientId}`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }
}

/**
 * Additional notification query/use case types (e.g., listing or counting notifications)
 * can be added here in the future as needed.
 */