import { beforeEach, describe, expect, it, vi } from 'vitest';

const dbQuery = vi.fn();

vi.mock('../../db', () => ({ default: { query: dbQuery } }));

describe('insight-source', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('queries the live SharpMove and LineMovement schemas with game-level team filters', async () => {
    dbQuery
      .mockResolvedValueOnce({ rows: [{ market: 'total', isSteamMove: true }] })
      .mockResolvedValueOnce({ rows: [{ marketType: 'total', steamMove: true }] });

    const { fetchInsightSourceData } = await import('../insight-source');
    const result = await fetchInsightSourceData({
      homeTeam: 'Cincinnati Reds',
      awayTeam: 'Boston Red Sox',
      league: 'mlb',
    });

    expect(result).toEqual({
      sharpMoves: [{ market: 'total', isSteamMove: true }],
      lineMovements: [{ marketType: 'total', steamMove: true }],
    });
    expect(String(dbQuery.mock.calls[0]?.[0] || '')).toContain('"moveDetectedAt"');
    expect(String(dbQuery.mock.calls[0]?.[0] || '')).toContain('"isSteamMove"');
    expect(String(dbQuery.mock.calls[1]?.[0] || '')).toContain('"recordedAt"');
    expect(String(dbQuery.mock.calls[1]?.[0] || '')).toContain('"steamMove"');
    expect(dbQuery.mock.calls[0]?.[1]).toEqual(['mlb', '%Cincinnati Reds%', '%Boston Red Sox%']);
    expect(dbQuery.mock.calls[1]?.[1]).toEqual(['mlb', '%Cincinnati Reds%', '%Boston Red Sox%']);
  });
});
