import { MediaReference } from '@core/domain/media/MediaReference';
import { describe, expect, it } from 'vitest';
import { RacingDriverFactory } from './RacingDriverFactory';

describe('RacingDriverFactory', () => {
  describe('getDriverAvatarRef', () => {
    it('should return deterministic MediaReference based on driver ID', () => {
      const factory = new RacingDriverFactory(10, new Date(), 'inmemory');
      
      // Test deterministic behavior
      const ref1 = factory.getDriverAvatarRef('driver-1');
      const ref2 = factory.getDriverAvatarRef('driver-1');
      
      expect(ref1.equals(ref2)).toBe(true);
      expect(ref1.type).toBe('system-default');
      expect(ref1.variant).toBe('avatar');
    });

    it('should produce different refs for different IDs', () => {
      const factory = new RacingDriverFactory(10, new Date(), 'inmemory');
      
      const ref1 = factory.getDriverAvatarRef('driver-1');
      const ref2 = factory.getDriverAvatarRef('driver-2');
      
      // They should be different refs (though both are system-default avatar)
      // The hash will be different
      expect(ref1.hash()).not.toBe(ref2.hash());
    });

    it('should use hash % 3 for variant selection', () => {
      const factory = new RacingDriverFactory(10, new Date(), 'inmemory');
      
      // Test multiple IDs to ensure distribution
      const refs = [
        factory.getDriverAvatarRef('driver-1'),
        factory.getDriverAvatarRef('driver-2'),
        factory.getDriverAvatarRef('driver-3'),
        factory.getDriverAvatarRef('driver-4'),
        factory.getDriverAvatarRef('driver-5'),
      ];

      // All should be system-default avatar
      refs.forEach(ref => {
        expect(ref.type).toBe('system-default');
        expect(ref.variant).toBe('avatar');
      });
    });
  });

  describe('create', () => {
    it('should create drivers with avatarRef set', () => {
      const factory = new RacingDriverFactory(5, new Date(), 'inmemory');
      const drivers = factory.create();
      
      expect(drivers.length).toBe(5);
      
      drivers.forEach(driver => {
        expect(driver.avatarRef).toBeDefined();
        expect(driver.avatarRef instanceof MediaReference).toBe(true);
        expect(driver.avatarRef.type).toBe('system-default');
        expect(driver.avatarRef.variant).toBe('avatar');
      });
    });

    it('should create deterministic drivers', () => {
      const factory1 = new RacingDriverFactory(3, new Date('2024-01-01'), 'inmemory');
      const factory2 = new RacingDriverFactory(3, new Date('2024-01-01'), 'inmemory');
      
      const drivers1 = factory1.create();
      const drivers2 = factory2.create();
      
      expect(drivers1.length).toBe(drivers2.length);
      
      for (let i = 0; i < drivers1.length; i++) {
        const driver1 = drivers1[i]!;
        const driver2 = drivers2[i]!;
        expect(driver1.id).toBe(driver2.id);
        expect(driver1.avatarRef.equals(driver2.avatarRef)).toBe(true);
      }
    });
  });
});