import { Sponsor } from '@core/racing/domain/entities/sponsor/Sponsor';
import type { Logger } from '@core/shared/domain/Logger';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { InMemorySponsorRepository } from './InMemorySponsorRepository';

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

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

  const createTestSponsor = (id: string, name: string, contactEmail: string) => {
    return Sponsor.create({
      id,
      name,
      contactEmail,
    });
  };

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

    it('should seed initial sponsors', () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      new InMemorySponsorRepository(mockLogger, [sponsor]);
      expect(mockLogger.debug).toHaveBeenCalledWith(`Seeded sponsor: 1 (Test Sponsor).`);
    });
  });

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

    it('should return the sponsor if found', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await repository.create(sponsor);
      const result = await repository.findById('1');
      expect(result).toEqual(sponsor);
      expect(mockLogger.info).toHaveBeenCalledWith('Found sponsor by ID: 1.');
    });
  });

  describe('findAll', () => {
    it('should return all sponsors', async () => {
      const sponsor1 = createTestSponsor('1', 'Sponsor 1', 's1@example.com');
      const sponsor2 = createTestSponsor('2', 'Sponsor 2', 's2@example.com');
      await repository.create(sponsor1);
      await repository.create(sponsor2);
      const result = await repository.findAll();
      expect(result).toHaveLength(2);
      expect(result).toContain(sponsor1);
      expect(result).toContain(sponsor2);
    });
  });

  describe('findByEmail', () => {
    it('should return null if sponsor not found', async () => {
      const result = await repository.findByEmail('nonexistent@example.com');
      expect(result).toBeNull();
      expect(mockLogger.debug).toHaveBeenCalledWith('[InMemorySponsorRepository] Finding sponsor by email: nonexistent@example.com');
      expect(mockLogger.warn).toHaveBeenCalledWith('Sponsor with email nonexistent@example.com not found.');
    });

    it('should return the sponsor if found', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await repository.create(sponsor);
      const result = await repository.findByEmail('TEST@EXAMPLE.COM');
      expect(result).toEqual(sponsor);
    });
  });

  describe('create', () => {
    it('should create a new sponsor', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      const result = await repository.create(sponsor);
      expect(result).toEqual(sponsor);
      expect(mockLogger.info).toHaveBeenCalledWith('Sponsor 1 (Test Sponsor) created successfully.');
    });

    it('should throw error if sponsor already exists', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await repository.create(sponsor);
      await expect(repository.create(sponsor)).rejects.toThrow('Sponsor already exists');
    });

    it('should throw error if email already exists', async () => {
      const sponsor1 = createTestSponsor('1', 'Sponsor 1', 'test@example.com');
      const sponsor2 = createTestSponsor('2', 'Sponsor 2', 'test@example.com');
      await repository.create(sponsor1);
      await expect(repository.create(sponsor2)).rejects.toThrow('Sponsor with this email already exists');
    });
  });

  describe('update', () => {
    it('should update an existing sponsor', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await repository.create(sponsor);
      const updatedSponsor = sponsor.update({ name: 'Updated Sponsor' });
      const result = await repository.update(updatedSponsor);
      expect(result).toEqual(updatedSponsor);
      expect(mockLogger.info).toHaveBeenCalledWith('Sponsor 1 (Updated Sponsor) updated successfully.');
    });

    it('should throw error if sponsor does not exist', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await expect(repository.update(sponsor)).rejects.toThrow('Sponsor not found');
    });

    it('should update email index when email changes', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await repository.create(sponsor);
      const updatedSponsor = sponsor.update({ contactEmail: 'new@example.com' });
      await repository.update(updatedSponsor);
      const found = await repository.findByEmail('new@example.com');
      expect(found).toEqual(updatedSponsor);
    });

    it('should throw error if updating to existing email', async () => {
      const sponsor1 = createTestSponsor('1', 'Sponsor 1', 'test1@example.com');
      const sponsor2 = createTestSponsor('2', 'Sponsor 2', 'test2@example.com');
      await repository.create(sponsor1);
      await repository.create(sponsor2);
      const updatedSponsor1 = sponsor1.update({ contactEmail: 'test2@example.com' });
      await expect(repository.update(updatedSponsor1)).rejects.toThrow('Sponsor with this email already exists');
    });
  });

  describe('delete', () => {
    it('should delete an existing sponsor', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await repository.create(sponsor);
      await repository.delete('1');
      expect(mockLogger.info).toHaveBeenCalledWith('Sponsor 1 deleted successfully.');
      const found = await repository.findById('1');
      expect(found).toBeNull();
    });

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

  describe('exists', () => {
    it('should return true if sponsor exists', async () => {
      const sponsor = createTestSponsor('1', 'Test Sponsor', 'test@example.com');
      await repository.create(sponsor);
      const result = await repository.exists('1');
      expect(result).toBe(true);
    });

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