/**
 * In-Memory Implementation: InMemoryNotificationRepository
 * 
 * Provides an in-memory storage implementation for notifications.
 */

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

export class InMemoryNotificationRepository implements INotificationRepository {
  private notifications: Map<string, Notification> = new Map();
  private readonly logger: Logger;

  constructor(logger: Logger, initialNotifications: Notification[] = []) {
    this.logger = logger;
    this.logger.info('InMemoryNotificationRepository initialized.');
    initialNotifications.forEach(notification => {
      this.notifications.set(notification.id, notification);
      this.logger.debug(`Seeded notification: ${notification.id}`);
    });
  }

  async findById(id: string): Promise<Notification | null> {
    this.logger.debug(`Finding notification by ID: ${id}`);
    try {
      const notification = this.notifications.get(id) || null;
      if (notification) {
        this.logger.info(`Found notification with ID: ${id}`);
      } else {
        this.logger.warn(`Notification with ID ${id} not found.`);
      }
      return notification;
    } catch (error) {
      this.logger.error(`Error finding notification by ID ${id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByRecipientId(recipientId: string): Promise<Notification[]> {
    this.logger.debug(`Finding notifications for recipient ID: ${recipientId}`);
    try {
      const notifications = Array.from(this.notifications.values())
        .filter(n => n.recipientId === recipientId)
        .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
      this.logger.info(`Found ${notifications.length} notifications for recipient ID: ${recipientId}.`);
      return notifications;
    } catch (error) {
      this.logger.error(`Error finding notifications for recipient ID ${recipientId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findUnreadByRecipientId(recipientId: string): Promise<Notification[]> {
    this.logger.debug(`Finding unread notifications for recipient ID: ${recipientId}`);
    try {
      const notifications = Array.from(this.notifications.values())
        .filter(n => n.recipientId === recipientId && n.isUnread())
        .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
      this.logger.info(`Found ${notifications.length} unread notifications for recipient ID: ${recipientId}.`);
      return notifications;
    } catch (error) {
      this.logger.error(`Error finding unread notifications for recipient ID ${recipientId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByRecipientIdAndType(recipientId: string, type: NotificationType): Promise<Notification[]> {
    this.logger.debug(`Finding notifications for recipient ID: ${recipientId}, type: ${type}`);
    try {
      const notifications = Array.from(this.notifications.values())
        .filter(n => n.recipientId === recipientId && n.type === type)
        .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
      this.logger.info(`Found ${notifications.length} notifications for recipient ID: ${recipientId}, type: ${type}.`);
      return notifications;
    } catch (error) {
      this.logger.error(`Error finding notifications for recipient ID ${recipientId}, type ${type}:`, error);
      throw error;
    }
  }

  async countUnreadByRecipientId(recipientId: string): Promise<number> {
    this.logger.debug(`Counting unread notifications for recipient ID: ${recipientId}`);
    try {
      const count = Array.from(this.notifications.values())
        .filter(n => n.recipientId === recipientId && n.isUnread())
        .length;
      this.logger.info(`Counted ${count} unread notifications for recipient ID: ${recipientId}.`);
      return count;
    } catch (error) {
      this.logger.error(`Error counting unread notifications for recipient ID ${recipientId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async create(notification: Notification): Promise<void> {
    this.logger.debug(`Creating notification: ${notification.id}`);
    try {
      if (this.notifications.has(notification.id)) {
        this.logger.warn(`Notification with ID ${notification.id} already exists. Throwing error.`);
        throw new Error(`Notification with ID ${notification.id} already exists`);
      }
      this.notifications.set(notification.id, notification);
      this.logger.info(`Notification ${notification.id} created successfully.`);
    } catch (error) {
      this.logger.error(`Error creating notification ${notification.id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async update(notification: Notification): Promise<void> {
    this.logger.debug(`Updating notification: ${notification.id}`);
    try {
      if (!this.notifications.has(notification.id)) {
        this.logger.warn(`Notification with ID ${notification.id} not found for update. Throwing error.`);
        throw new Error(`Notification with ID ${notification.id} not found`);
      }
      this.notifications.set(notification.id, notification);
      this.logger.info(`Notification ${notification.id} updated successfully.`);
    } catch (error) {
      this.logger.error(`Error updating notification ${notification.id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async delete(id: string): Promise<void> {
    this.logger.debug(`Deleting notification: ${id}`);
    try {
      if (this.notifications.delete(id)) {
        this.logger.info(`Notification ${id} deleted successfully.`);
      } else {
        this.logger.warn(`Notification with ID ${id} not found for deletion.`);
      }
    } catch (error) {
      this.logger.error(`Error deleting notification ${id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async deleteAllByRecipientId(recipientId: string): Promise<void> {
    this.logger.debug(`Deleting all notifications for recipient ID: ${recipientId}`);
    try {
      const initialCount = this.notifications.size;
      const toDelete = Array.from(this.notifications.values())
        .filter(n => n.recipientId === recipientId)
        .map(n => n.id);
      
      toDelete.forEach(id => this.notifications.delete(id));
      this.logger.info(`Deleted ${toDelete.length} notifications for recipient ID: ${recipientId}.`);
    } catch (error) {
      this.logger.error(`Error deleting all notifications for recipient ID ${recipientId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async markAllAsReadByRecipientId(recipientId: string): Promise<void> {
    this.logger.debug(`Marking all notifications as read for recipient ID: ${recipientId}`);
    try {
      const toUpdate = Array.from(this.notifications.values())
        .filter(n => n.recipientId === recipientId && n.isUnread());
      
      this.logger.info(`Found ${toUpdate.length} unread notifications to mark as read for recipient ID: ${recipientId}.`);

      toUpdate.forEach(n => {
        const updated = n.markAsRead();
        this.notifications.set(updated.id, updated);
      });
      this.logger.info(`Marked ${toUpdate.length} notifications as read for recipient ID: ${recipientId}.`);
    } catch (error) {
      this.logger.error(`Error marking all notifications as read for recipient ID ${recipientId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }
}