import { InquiryEntity } from '../entities/inquiry.entity';
import { InquiryCreatedEvent } from '../events/inquiry-created.event';
import { InquiryReadEvent } from '../events/inquiry-read.event';

describe('InquiryEntity', () => {
  describe('createNew', () => {
    it('creates an inquiry with correct properties', () => {
      const inquiry = InquiryEntity.createNew(
        'inq-1',
        'listing-1',
        'user-1',
        'Tôi muốn xem nhà',
        '0901234567',
      );

      expect(inquiry.id).toBe('inq-1');
      expect(inquiry.listingId).toBe('listing-1');
      expect(inquiry.userId).toBe('user-1');
      expect(inquiry.message).toBe('Tôi muốn xem nhà');
      expect(inquiry.phone).toBe('0901234567');
      expect(inquiry.isRead).toBe(false);
    });

    it('creates an inquiry with null phone', () => {
      const inquiry = InquiryEntity.createNew(
        'inq-2',
        'listing-1',
        'user-1',
        'Cho tôi hỏi giá',
        null,
      );

      expect(inquiry.phone).toBeNull();
    });

    it('emits InquiryCreatedEvent', () => {
      const inquiry = InquiryEntity.createNew(
        'inq-1',
        'listing-1',
        'user-1',
        'Tôi muốn xem nhà',
        '0901234567',
      );

      const events = inquiry.domainEvents;
      expect(events).toHaveLength(1);
      expect(events[0]).toBeInstanceOf(InquiryCreatedEvent);

      const event = events[0] as InquiryCreatedEvent;
      expect(event.aggregateId).toBe('inq-1');
      expect(event.listingId).toBe('listing-1');
      expect(event.userId).toBe('user-1');
    });
  });

  describe('markAsRead', () => {
    it('sets isRead to true', () => {
      const inquiry = InquiryEntity.createNew(
        'inq-1',
        'listing-1',
        'user-1',
        'Tôi muốn xem nhà',
        null,
      );

      expect(inquiry.isRead).toBe(false);
      inquiry.markAsRead();
      expect(inquiry.isRead).toBe(true);
    });

    it('emits InquiryReadEvent', () => {
      const inquiry = InquiryEntity.createNew(
        'inq-1',
        'listing-1',
        'user-1',
        'Tôi muốn xem nhà',
        null,
      );

      // Clear the creation event
      inquiry.clearDomainEvents();

      inquiry.markAsRead();

      const events = inquiry.domainEvents;
      expect(events).toHaveLength(1);
      expect(events[0]).toBeInstanceOf(InquiryReadEvent);

      const event = events[0] as InquiryReadEvent;
      expect(event.aggregateId).toBe('inq-1');
      expect(event.listingId).toBe('listing-1');
      expect(event.userId).toBe('user-1');
    });
  });
});
