/**
 * Infrastructure: InMemoryEngagementRepository
 * 
 * In-memory implementation of IEngagementRepository for development/testing.
 */

import type { IEngagementRepository } from '@core/analytics/domain/repositories/IEngagementRepository';
import { EngagementEvent, type EngagementAction, type EngagementEntityType } from '@core/analytics/domain/entities/EngagementEvent';
import { Logger } from '@core/shared/application';


export class InMemoryEngagementRepository implements IEngagementRepository {
  private events: Map<string, EngagementEvent> = new Map();
  private logger: Logger;

  constructor(logger: Logger) {
    this.logger = logger;
    this.logger.info('InMemoryEngagementRepository initialized.');
  }

  async save(event: EngagementEvent): Promise<void> {
    this.logger.debug(`Attempting to save engagement event: ${event.id}`);
    try {
      this.events.set(event.id, event);
      this.logger.info(`Successfully saved engagement event: ${event.id}`);
    } catch (error) {
      this.logger.error(`Error saving engagement event ${event.id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findById(id: string): Promise<EngagementEvent | null> {
    this.logger.debug(`Attempting to find engagement event by ID: ${id}`);
    try {
      const event = this.events.get(id) ?? null;
      if (event) {
        this.logger.info(`Found engagement event by ID: ${id}`);
      } else {
        this.logger.warn(`Engagement event not found for ID: ${id}`);
        // The original was info, but if a requested ID is not found that's more of a warning than an info.
      }
      return event;
    } catch (error) {
      this.logger.error(`Error finding engagement event by ID ${id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByEntityId(entityType: EngagementEntityType, entityId: string): Promise<EngagementEvent[]> {
    this.logger.debug(`Attempting to find engagement events for entityType: ${entityType}, entityId: ${entityId}`);
    try {
      const events = Array.from(this.events.values()).filter(
        e => e.entityType === entityType && e.entityId === entityId
      );
      this.logger.info(`Found ${events.length} engagement events for entityType: ${entityType}, entityId: ${entityId}`);
      return events;
    } catch (error) {
      this.logger.error(`Error finding engagement events by entity ID ${entityId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByAction(action: EngagementAction): Promise<EngagementEvent[]> {
    this.logger.debug(`Attempting to find engagement events by action: ${action}`);
    try {
      const events = Array.from(this.events.values()).filter(
        e => e.action === action
      );
      this.logger.info(`Found ${events.length} engagement events for action: ${action}`);
      return events;
    } catch (error) {
      this.logger.error(`Error finding engagement events by action ${action}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByDateRange(startDate: Date, endDate: Date): Promise<EngagementEvent[]> {
    this.logger.debug(`Attempting to find engagement events by date range: ${startDate.toISOString()} - ${endDate.toISOString()}`);
    try {
      const events = Array.from(this.events.values()).filter(
        e => e.timestamp >= startDate && e.timestamp <= endDate
      );
      this.logger.info(`Found ${events.length} engagement events for date range.`);
      return events;
    } catch (error) {
      this.logger.error(`Error finding engagement events by date range:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async countByAction(action: EngagementAction, entityId?: string, since?: Date): Promise<number> {
    this.logger.debug(`Attempting to count engagement events for action: ${action}, entityId: ${entityId}, since: ${since?.toISOString()}`);
    try {
      const count = Array.from(this.events.values()).filter(
        e => e.action === action &&
             (!entityId || e.entityId === entityId) &&
             (!since || e.timestamp >= since)
      ).length;
      this.logger.info(`Counted ${count} engagement events for action: ${action}, entityId: ${entityId}`);
      return count;
    } catch (error) {
      this.logger.error(`Error counting engagement events by action ${action}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async getSponsorClicksForEntity(entityId: string, since?: Date): Promise<number> {
    this.logger.debug(`Attempting to get sponsor clicks for entity ID: ${entityId}, since: ${since?.toISOString()}`);
    try {
      const count = Array.from(this.events.values()).filter(
        e => e.entityId === entityId &&
             (e.action === 'click_sponsor_logo' || e.action === 'click_sponsor_url') &&
             (!since || e.timestamp >= since)
      ).length;
      this.logger.info(`Counted ${count} sponsor clicks for entity ID: ${entityId}`);
      return count;
    } catch (error) {
      this.logger.error(`Error getting sponsor clicks for entity ID ${entityId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  // Helper for testing
  clear(): void {
    this.logger.debug('Clearing all engagement events.');
    this.events.clear();
    this.logger.info('All engagement events cleared.');
  }

  // Helper for seeding demo data
  seed(events: EngagementEvent[]): void {
    this.logger.debug(`Seeding ${events.length} engagement events.`);
    try {
      events.forEach(e => this.events.set(e.id, e));
      this.logger.info(`Successfully seeded ${events.length} engagement events.`);
    } catch (error) {
      this.logger.error(`Error seeding engagement events:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }
}