const mockQuery = jest.fn();
const mockLoggerInfo = jest.fn();

jest.mock('../database/client', () => ({
  getDatabasePool: () => ({ query: mockQuery }),
}));

jest.mock('../utils/logger', () => ({
  logger: {
    info: mockLoggerInfo,
    warn: jest.fn(),
    error: jest.fn(),
  },
}));

import { OHLCVGenerator } from './ohlcv-generator';

describe('OHLCVGenerator', () => {
  beforeEach(() => {
    mockQuery.mockReset();
    mockLoggerInfo.mockReset();
  });

  it('returns empty data and logs once when swap tables are not provisioned yet', async () => {
    mockQuery.mockRejectedValue({
      code: '42P01',
      message: 'relation "swap_events" does not exist',
    });

    const generator = new OHLCVGenerator();

    await expect(
      generator.generateOHLCV(
        138,
        '0xToken',
        '1h',
        new Date('2026-03-01T00:00:00.000Z'),
        new Date('2026-03-02T00:00:00.000Z')
      )
    ).resolves.toEqual([]);

    await expect(
      generator.generateOHLCV(
        138,
        '0xToken',
        '1h',
        new Date('2026-03-01T00:00:00.000Z'),
        new Date('2026-03-02T00:00:00.000Z')
      )
    ).resolves.toEqual([]);

    expect(mockLoggerInfo).toHaveBeenCalledTimes(1);
    expect(mockLoggerInfo).toHaveBeenCalledWith(
      'Skipping OHLCV generation; relation "swap_events" is not available yet',
      expect.objectContaining({
        operation: 'OHLCV generation',
        relation: 'swap_events',
      })
    );
  });
});
