import { NotificationPreference } from '@core/notifications/domain/entities/NotificationPreference';
import type { Logger } from '@core/shared/domain/Logger';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { InMemoryNotificationPreferenceRepository } from './InMemoryNotificationPreferenceRepository';

describe('InMemoryNotificationPreferenceRepository', () => {
  let repository: InMemoryNotificationPreferenceRepository;
  let logger: Logger;

  beforeEach(() => {
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    } as unknown as Logger;

    repository = new InMemoryNotificationPreferenceRepository(logger);
  });

  it('returns default preferences when missing', async () => {
    const pref = await repository.getOrCreateDefault('driver-1');
    expect(pref).toBeInstanceOf(NotificationPreference);
    expect(pref.driverId).toBe('driver-1');

    const found = await repository.findByDriverId('driver-1');
    expect(found?.driverId).toBe('driver-1');
  });

  it('saves and deletes', async () => {
    const pref = NotificationPreference.createDefault('driver-2');
    await repository.save(pref);

    expect((await repository.findByDriverId('driver-2'))?.driverId).toBe('driver-2');

    await repository.delete('driver-2');
    expect(await repository.findByDriverId('driver-2')).toBeNull();
  });
});
