import { AggregateRoot } from '@modules/shared';
import { ReviewCreatedEvent } from '../events/review-created.event';
import { ReviewDeletedEvent } from '../events/review-deleted.event';
import { type Rating } from '../value-objects/rating.vo';

export interface ReviewProps {
  userId: string;
  targetType: string;
  targetId: string;
  rating: Rating;
  comment: string | null;
}

export class ReviewEntity extends AggregateRoot<string> {
  private _userId: string;
  private _targetType: string;
  private _targetId: string;
  private _rating: Rating;
  private _comment: string | null;

  constructor(id: string, props: ReviewProps, createdAt?: Date) {
    super(id, createdAt);
    this._userId = props.userId;
    this._targetType = props.targetType;
    this._targetId = props.targetId;
    this._rating = props.rating;
    this._comment = props.comment;
  }

  get userId(): string { return this._userId; }
  get targetType(): string { return this._targetType; }
  get targetId(): string { return this._targetId; }
  get rating(): Rating { return this._rating; }
  get comment(): string | null { return this._comment; }

  static createNew(
    id: string,
    userId: string,
    targetType: string,
    targetId: string,
    rating: Rating,
    comment: string | null,
  ): ReviewEntity {
    const review = new ReviewEntity(id, {
      userId,
      targetType,
      targetId,
      rating,
      comment,
    });

    review.addDomainEvent(
      new ReviewCreatedEvent(id, userId, targetType, targetId, rating.value),
    );
    return review;
  }

  markDeleted(): void {
    this.addDomainEvent(
      new ReviewDeletedEvent(this.id, this._userId, this._targetType, this._targetId),
    );
  }
}
