import { describe, it, expect } from 'vitest';
import { PropertyMediaEntity } from '../entities/property-media.entity';

describe('PropertyMediaEntity', () => {
  it('should create media via constructor with all properties', () => {
    const media = new PropertyMediaEntity('media-1', {
      propertyId: 'prop-1',
      url: 'https://cdn.example.com/photo.jpg',
      type: 'image',
      order: 0,
      caption: 'Mặt tiền nhà',
      aiTags: { tags: ['exterior'] },
    });

    expect(media.id).toBe('media-1');
    expect(media.propertyId).toBe('prop-1');
    expect(media.url).toBe('https://cdn.example.com/photo.jpg');
    expect(media.type).toBe('image');
    expect(media.order).toBe(0);
    expect(media.caption).toBe('Mặt tiền nhà');
    expect(media.aiTags).toEqual({ tags: ['exterior'] });
    expect(media.createdAt).toBeInstanceOf(Date);
  });

  it('should create media via createNew factory with caption', () => {
    const media = PropertyMediaEntity.createNew(
      'media-2',
      'prop-1',
      'https://cdn.example.com/interior.jpg',
      'image',
      1,
      'Phòng khách',
    );

    expect(media.id).toBe('media-2');
    expect(media.propertyId).toBe('prop-1');
    expect(media.url).toBe('https://cdn.example.com/interior.jpg');
    expect(media.type).toBe('image');
    expect(media.order).toBe(1);
    expect(media.caption).toBe('Phòng khách');
    expect(media.aiTags).toBeNull();
  });

  it('should create media via createNew factory without caption', () => {
    const media = PropertyMediaEntity.createNew(
      'media-3',
      'prop-2',
      'https://cdn.example.com/video.mp4',
      'video',
      0,
    );

    expect(media.type).toBe('video');
    expect(media.caption).toBeNull();
    expect(media.aiTags).toBeNull();
  });

  it('should preserve order values', () => {
    const media0 = PropertyMediaEntity.createNew('m-0', 'p-1', 'http://a.com/0.jpg', 'image', 0);
    const media5 = PropertyMediaEntity.createNew('m-5', 'p-1', 'http://a.com/5.jpg', 'image', 5);

    expect(media0.order).toBe(0);
    expect(media5.order).toBe(5);
  });

  it('should accept custom createdAt via constructor', () => {
    const pastDate = new Date('2024-01-01T00:00:00Z');
    const media = new PropertyMediaEntity(
      'media-old',
      {
        propertyId: 'prop-1',
        url: 'https://cdn.example.com/old.jpg',
        type: 'image',
        order: 0,
        caption: null,
        aiTags: null,
      },
      pastDate,
    );

    expect(media.createdAt).toBe(pastDate);
  });
});
