import { AggregateRoot } from '@modules/shared';
import { InquiryCreatedEvent } from '../events/inquiry-created.event';
import { InquiryReadEvent } from '../events/inquiry-read.event';

export interface InquiryProps {
  listingId: string;
  userId: string;
  message: string;
  phone: string | null;
  isRead: boolean;
}

export class InquiryEntity extends AggregateRoot<string> {
  private _listingId: string;
  private _userId: string;
  private _message: string;
  private _phone: string | null;
  private _isRead: boolean;

  constructor(id: string, props: InquiryProps, createdAt?: Date) {
    super(id, createdAt);
    this._listingId = props.listingId;
    this._userId = props.userId;
    this._message = props.message;
    this._phone = props.phone;
    this._isRead = props.isRead;
  }

  get listingId(): string { return this._listingId; }
  get userId(): string { return this._userId; }
  get message(): string { return this._message; }
  get phone(): string | null { return this._phone; }
  get isRead(): boolean { return this._isRead; }

  static createNew(
    id: string,
    listingId: string,
    userId: string,
    message: string,
    phone: string | null,
  ): InquiryEntity {
    const inquiry = new InquiryEntity(id, {
      listingId,
      userId,
      message,
      phone,
      isRead: false,
    });

    inquiry.addDomainEvent(
      new InquiryCreatedEvent(id, listingId, userId),
    );
    return inquiry;
  }

  markAsRead(): void {
    this._isRead = true;
    this.addDomainEvent(
      new InquiryReadEvent(this.id, this._listingId, this._userId),
    );
  }
}
