/**
 * Application Use Case: MarkNotificationReadUseCase
 * 
 * Marks a notification as read.
 */

import type { Logger } from '@core/shared/domain/Logger';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { NotificationRepository } from '../../domain/repositories/NotificationRepository';

export interface MarkNotificationReadCommand {
  notificationId: string;
  recipientId: string;
}

export interface MarkNotificationReadResult {
  notificationId: string;
  recipientId: string;
  wasAlreadyRead: boolean;
}

export type MarkNotificationReadErrorCode =
  | 'NOTIFICATION_NOT_FOUND'
  | 'RECIPIENT_MISMATCH'
  | 'REPOSITORY_ERROR';

export class MarkNotificationReadUseCase {
  constructor(
    private readonly notificationRepository: NotificationRepository,
    private readonly logger: Logger,
  ) {}

  async execute(
    command: MarkNotificationReadCommand,
  ): Promise<Result<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>> {
    this.logger.debug(
      `Attempting to mark notification ${command.notificationId} as read for recipient ${command.recipientId}`,
    );

    try {
      const notification = await this.notificationRepository.findById(command.notificationId);

      if (!notification) {
        this.logger.warn(`Notification not found for ID: ${command.notificationId}`);
        return Result.err<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
          code: 'NOTIFICATION_NOT_FOUND',
          details: { message: 'Notification not found' },
        });
      }

      if (notification.recipientId !== command.recipientId) {
        this.logger.warn(
          `Unauthorized attempt to mark notification ${command.notificationId}. Recipient ID mismatch.`,
        );
        return Result.err<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
          code: 'RECIPIENT_MISMATCH',
          details: { message: "Cannot mark another user's notification as read" },
        });
      }

      if (!notification.isUnread()) {
        this.logger.info(
          `Notification ${command.notificationId} is already read. Skipping update.`,
        );
        return Result.ok<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
          notificationId: command.notificationId,
          recipientId: command.recipientId,
          wasAlreadyRead: true,
        });
      }

      const updatedNotification = notification.markAsRead();
      await this.notificationRepository.update(updatedNotification);
      this.logger.info(
        `Notification ${command.notificationId} successfully marked as read.`,
      );

      return Result.ok<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
        notificationId: command.notificationId,
        recipientId: command.recipientId,
        wasAlreadyRead: false,
      });
    } catch (error) {
      const err = error instanceof Error ? error : new Error(String(error));
      this.logger.error(
        `Failed to mark notification ${command.notificationId} as read: ${err.message}`,
      );
      return Result.err<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
        code: 'REPOSITORY_ERROR',
        details: { message: err.message },
      });
    }
  }
}

/**
 * Application Use Case: MarkAllNotificationsReadUseCase
 * 
 * Marks all notifications as read for a recipient.
 */
export interface MarkAllNotificationsReadInput {
  recipientId: string;
}

export interface MarkAllNotificationsReadResult {
  recipientId: string;
}

export type MarkAllNotificationsReadErrorCode = 'REPOSITORY_ERROR';

export class MarkAllNotificationsReadUseCase {
  constructor(
    private readonly notificationRepository: NotificationRepository,
  ) {}

  async execute(
    input: MarkAllNotificationsReadInput,
  ): Promise<Result<MarkAllNotificationsReadResult, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>> {
    try {
      await this.notificationRepository.markAllAsReadByRecipientId(input.recipientId);
      return Result.ok<MarkAllNotificationsReadResult, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>({
        recipientId: input.recipientId,
      });
    } catch (error) {
      const err = error instanceof Error ? error : new Error(String(error));
      return Result.err<MarkAllNotificationsReadResult, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>({
        code: 'REPOSITORY_ERROR',
        details: { message: err.message },
      });
    }
  }
}

/**
 * Application Use Case: DismissNotificationUseCase
 * 
 * Dismisses a notification.
 */
export interface DismissNotificationCommand {
  notificationId: string;
  recipientId: string;
}

export interface DismissNotificationResult {
  notificationId: string;
  recipientId: string;
  wasAlreadyDismissed: boolean;
}

export type DismissNotificationErrorCode =
  | 'NOTIFICATION_NOT_FOUND'
  | 'RECIPIENT_MISMATCH'
  | 'CANNOT_DISMISS_REQUIRING_RESPONSE'
  | 'REPOSITORY_ERROR';

export class DismissNotificationUseCase {
  constructor(
    private readonly notificationRepository: NotificationRepository,
  ) {}

  async execute(
    command: DismissNotificationCommand,
  ): Promise<Result<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>> {
    try {
      const notification = await this.notificationRepository.findById(
        command.notificationId,
      );

      if (!notification) {
        return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
          code: 'NOTIFICATION_NOT_FOUND',
          details: { message: 'Notification not found' },
        });
      }

      if (notification.recipientId !== command.recipientId) {
        return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
          code: 'RECIPIENT_MISMATCH',
          details: { message: "Cannot dismiss another user's notification" },
        });
      }

      if (notification.isDismissed()) {
        return Result.ok<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
          notificationId: command.notificationId,
          recipientId: command.recipientId,
          wasAlreadyDismissed: true,
        });
      }

      if (!notification.canDismiss()) {
        return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
          code: 'CANNOT_DISMISS_REQUIRING_RESPONSE',
          details: { message: 'Cannot dismiss notification that requires response' },
        });
      }

      const updatedNotification = notification.dismiss();
      await this.notificationRepository.update(updatedNotification);

      return Result.ok<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
        notificationId: command.notificationId,
        recipientId: command.recipientId,
        wasAlreadyDismissed: false,
      });
    } catch (error) {
      const err = error instanceof Error ? error : new Error(String(error));
      return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
        code: 'REPOSITORY_ERROR',
        details: { message: err.message },
      });
    }
  }
}
