import { type ListingStatus } from '@prisma/client';
import { type ListingEntity } from '../entities/listing.entity';

export interface ModerationAction {
  action: 'approve' | 'reject';
  moderationScore?: number;
  notes?: string;
}

export class ModerationService {
  /**
   * Apply a moderation action to a listing entity.
   * Mutates the listing in-place (score + status transition).
   */
  applyModeration(listing: ListingEntity, moderation: ModerationAction): void {
    if (moderation.moderationScore !== undefined) {
      listing.setModerationScore(moderation.moderationScore, moderation.notes);
    }

    if (moderation.action === 'approve') {
      listing.approve();
    } else {
      listing.reject(moderation.notes ?? 'Bị từ chối bởi moderator');
    }
  }

  /**
   * Apply a status transition that may be moderation-related.
   * Detects approve/reject patterns and delegates to entity methods.
   */
  applyStatusTransition(
    listing: ListingEntity,
    newStatus: ListingStatus,
    moderationNotes?: string,
  ): void {
    if (newStatus === 'REJECTED' && moderationNotes) {
      listing.reject(moderationNotes);
    } else if (newStatus === 'ACTIVE' && listing.status === 'PENDING_REVIEW') {
      listing.approve();
    } else {
      listing.transitionTo(newStatus);
    }
  }
}
