import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Logger } from '@core/shared/application';
import type { RacingSeedData } from './InMemorySocialAndFeed';
import { InMemoryFeedRepository, InMemorySocialGraphRepository } from './InMemorySocialAndFeed';

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

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

  it('builds feed for a driver based on friendships', async () => {
    const seed = {
      drivers: [{ id: 'd1' }, { id: 'd2' }, { id: 'd3' }],
      friendships: [
        { driverId: 'd1', friendId: 'd2' },
        { driverId: 'd1', friendId: 'd3' },
      ],
      feedEvents: [
        { id: 'f1', actorDriverId: 'd2', timestamp: new Date('2025-01-02T00:00:00.000Z') },
        { id: 'f2', actorDriverId: 'd3', timestamp: new Date('2025-01-03T00:00:00.000Z') },
        { id: 'f3', actorDriverId: 'd1', timestamp: new Date('2025-01-04T00:00:00.000Z') },
      ],
    };

    const feedRepo = new InMemoryFeedRepository(logger, seed as unknown as RacingSeedData);
    const feed = await feedRepo.getFeedForDriver('d1');
    expect(feed.map(f => (f as { id: string }).id)).toEqual(['f2', 'f1']);

    const global = await feedRepo.getGlobalFeed(2);
    expect(global.length).toBe(2);
  });

  it('returns friends and suggestions', async () => {
    const seed = {
      drivers: [{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }],
      friendships: [
        { driverId: 'a', friendId: 'b' },
        { driverId: 'b', friendId: 'c' },
        { driverId: 'a', friendId: 'd' },
        { driverId: 'd', friendId: 'c' },
      ],
      feedEvents: [],
    };

    const repo = new InMemorySocialGraphRepository(logger, seed as unknown as RacingSeedData);

    expect(await repo.getFriendIds('a')).toEqual(['b', 'd']);

    const friends = await repo.getFriends('a');
    expect(friends.map(d => (d as { id: string }).id).sort()).toEqual(['b', 'd']);

    const suggested = await repo.getSuggestedFriends('a');
    expect(suggested.map(d => (d as { id: string }).id)).toContain('c');
  });
});
