import { vi, describe, it, expect, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from './InMemoryDriverRepository';
import { Driver } from '@core/racing/domain/entities/Driver';
import type { Logger } from '@core/shared/application';
import { MediaReference } from '@core/domain/media/MediaReference';

describe('InMemoryDriverRepository', () => {
  let repository: InMemoryDriverRepository;
  let mockLogger: Logger;

  beforeEach(() => {
    mockLogger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    repository = new InMemoryDriverRepository(mockLogger);
  });

  const createTestDriver = (id: string, iracingId: string, name: string, country: string, avatarRef?: MediaReference) => {
    const props: {
      id: string;
      iracingId: string;
      name: string;
      country: string;
      avatarRef?: MediaReference;
    } = {
      id,
      iracingId,
      name,
      country,
    };
    if (avatarRef !== undefined) {
      props.avatarRef = avatarRef;
    }
    return Driver.create(props);
  };

  describe('constructor', () => {
    it('should initialize with a logger', () => {
      expect(repository).toBeDefined();
      expect(mockLogger.info).toHaveBeenCalledWith('InMemoryDriverRepository initialized.');
    });
  });

  describe('findById', () => {
    it('should return null if driver not found', async () => {
      const result = await repository.findById('nonexistent');
      expect(result).toBeNull();
      expect(mockLogger.debug).toHaveBeenCalledWith('[InMemoryDriverRepository] Finding driver by ID: nonexistent');
      expect(mockLogger.warn).toHaveBeenCalledWith('Driver with ID nonexistent not found.');
    });

    it('should return the driver if found', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);

      const result = await repository.findById('1');
      expect(result).toEqual(driver);
      expect(mockLogger.info).toHaveBeenCalledWith('Found driver by ID: 1.');
    });
  });

  describe('findByIRacingId', () => {
    it('should return null if driver not found', async () => {
      const result = await repository.findByIRacingId('nonexistent');
      expect(result).toBeNull();
      expect(mockLogger.debug).toHaveBeenCalledWith('[InMemoryDriverRepository] Finding driver by iRacing ID: nonexistent');
      expect(mockLogger.warn).toHaveBeenCalledWith('Driver with iRacing ID nonexistent not found.');
    });

    it('should return the driver if found', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);

      const result = await repository.findByIRacingId('12345');
      expect(result).toEqual(driver);
    });
  });

  describe('findAll', () => {
    it('should return all drivers', async () => {
      const driver1 = createTestDriver('1', '12345', 'Driver 1', 'US');
      const driver2 = createTestDriver('2', '67890', 'Driver 2', 'UK');
      await repository.create(driver1);
      await repository.create(driver2);

      const result = await repository.findAll();
      expect(result).toHaveLength(2);
      expect(result).toContain(driver1);
      expect(result).toContain(driver2);
    });
  });

  describe('create', () => {
    it('should create a new driver', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      const result = await repository.create(driver);
      expect(result).toEqual(driver);
      expect(mockLogger.info).toHaveBeenCalledWith('Driver 1 created successfully.');
    });

    it('should throw error if driver already exists', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);
      await expect(repository.create(driver)).rejects.toThrow('Driver already exists');
    });

    it('should throw error if iRacing ID already registered', async () => {
      const driver1 = createTestDriver('1', '12345', 'Driver 1', 'US');
      const driver2 = createTestDriver('2', '12345', 'Driver 2', 'UK');
      await repository.create(driver1);
      await expect(repository.create(driver2)).rejects.toThrow('iRacing ID already registered');
    });
  });

  describe('update', () => {
    it('should update an existing driver', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);

      const updatedDriver = driver.update({ name: 'Updated Driver' });
      const result = await repository.update(updatedDriver);
      expect(result).toEqual(updatedDriver);
      expect(mockLogger.info).toHaveBeenCalledWith('Driver 1 updated successfully.');
    });

    it('should throw error if driver does not exist', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await expect(repository.update(driver)).rejects.toThrow('Driver not found');
    });

    it('should re-index iRacing ID if changed', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);

      const updatedDriver = Driver.create({
        id: '1',
        iracingId: '67890',
        name: 'Test Driver',
        country: 'US',
      });
      await repository.update(updatedDriver);

      const foundByOldId = await repository.findByIRacingId('12345');
      expect(foundByOldId).toBeNull();

      const foundByNewId = await repository.findByIRacingId('67890');
      expect(foundByNewId).toEqual(updatedDriver);
    });
  });

  describe('delete', () => {
    it('should delete an existing driver', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);

      await repository.delete('1');
      expect(mockLogger.info).toHaveBeenCalledWith('Driver 1 deleted successfully.');
      const found = await repository.findById('1');
      expect(found).toBeNull();
      const foundByIRacingId = await repository.findByIRacingId('12345');
      expect(foundByIRacingId).toBeNull();
    });

    it('should not throw error if driver does not exist', async () => {
      await repository.delete('nonexistent');
      expect(mockLogger.warn).toHaveBeenCalledWith('Driver with ID nonexistent not found for deletion.');
    });
  });

  describe('exists', () => {
    it('should return true if driver exists', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);

      const result = await repository.exists('1');
      expect(result).toBe(true);
    });

    it('should return false if driver does not exist', async () => {
      const result = await repository.exists('nonexistent');
      expect(result).toBe(false);
    });
  });

  describe('existsByIRacingId', () => {
    it('should return true if iRacing ID exists', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
      await repository.create(driver);

      const result = await repository.existsByIRacingId('12345');
      expect(result).toBe(true);
    });

    it('should return false if iRacing ID does not exist', async () => {
      const result = await repository.existsByIRacingId('nonexistent');
      expect(result).toBe(false);
    });
  });

  describe('serialization with MediaReference', () => {
    it('should serialize driver with uploaded avatarRef', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US', MediaReference.createUploaded('media-123'));
      await repository.create(driver);

      const serialized = repository.serialize(driver);
      expect(serialized.avatarRef).toEqual({ type: 'uploaded', mediaId: 'media-123' });
    });

    it('should serialize driver with system-default avatarRef', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US', MediaReference.createSystemDefault('avatar'));
      await repository.create(driver);

      const serialized = repository.serialize(driver);
      expect(serialized.avatarRef).toEqual({ type: 'system-default', variant: 'avatar' });
    });

    it('should serialize driver with generated avatarRef', async () => {
      const driver = createTestDriver('1', '12345', 'Test Driver', 'US', MediaReference.createGenerated('gen-123'));
      await repository.create(driver);

      const serialized = repository.serialize(driver);
      expect(serialized.avatarRef).toEqual({ type: 'generated', generationRequestId: 'gen-123' });
    });

    it('should deserialize driver with uploaded avatarRef', () => {
      const data = {
        id: '1',
        iracingId: '12345',
        name: 'Test Driver',
        country: 'US',
        bio: null,
        joinedAt: new Date().toISOString(),
        category: null,
        avatarRef: { type: 'uploaded', mediaId: 'media-123' },
      };

      const driver = repository.deserialize(data);
      expect(driver.id).toBe('1');
      expect(driver.avatarRef.type).toBe('uploaded');
      expect(driver.avatarRef.mediaId).toBe('media-123');
    });

    it('should deserialize driver with system-default avatarRef', () => {
      const data = {
        id: '1',
        iracingId: '12345',
        name: 'Test Driver',
        country: 'US',
        bio: null,
        joinedAt: new Date().toISOString(),
        category: null,
        avatarRef: { type: 'system-default', variant: 'avatar' },
      };

      const driver = repository.deserialize(data);
      expect(driver.id).toBe('1');
      expect(driver.avatarRef.type).toBe('system-default');
      expect(driver.avatarRef.variant).toBe('avatar');
    });

    it('should deserialize driver with generated avatarRef', () => {
      const data = {
        id: '1',
        iracingId: '12345',
        name: 'Test Driver',
        country: 'US',
        bio: null,
        joinedAt: new Date().toISOString(),
        category: null,
        avatarRef: { type: 'generated', generationRequestId: 'gen-123' },
      };

      const driver = repository.deserialize(data);
      expect(driver.id).toBe('1');
      expect(driver.avatarRef.type).toBe('generated');
      expect(driver.avatarRef.generationRequestId).toBe('gen-123');
    });

    it('should deserialize driver without avatarRef (backward compatibility)', () => {
      const data = {
        id: '1',
        iracingId: '12345',
        name: 'Test Driver',
        country: 'US',
        bio: null,
        joinedAt: new Date().toISOString(),
        category: null,
      };

      const driver = repository.deserialize(data);
      expect(driver.id).toBe('1');
      expect(driver.avatarRef.type).toBe('system-default');
      expect(driver.avatarRef.variant).toBe('avatar');
    });

    it('should roundtrip serialize and deserialize with avatarRef', async () => {
      const originalDriver = createTestDriver('1', '12345', 'Test Driver', 'US', MediaReference.createUploaded('media-456'));
      await repository.create(originalDriver);

      const serialized = repository.serialize(originalDriver);
      const deserialized = repository.deserialize(serialized);

      expect(deserialized.id).toBe(originalDriver.id);
      expect(deserialized.iracingId.toString()).toBe(originalDriver.iracingId.toString());
      expect(deserialized.name.toString()).toBe(originalDriver.name.toString());
      expect(deserialized.country.toString()).toBe(originalDriver.country.toString());
      expect(deserialized.avatarRef.equals(originalDriver.avatarRef)).toBe(true);
    });
  });
});