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 { VolumeCalculator } from './volume-calculator';

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

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

    const calculator = new VolumeCalculator();

    await expect(calculator.calculateTokenVolume(138, '0xToken')).resolves.toEqual({
      volume5m: 0,
      volume1h: 0,
      volume24h: 0,
      volume7d: 0,
      volume30d: 0,
      txCount5m: 0,
      txCount1h: 0,
      txCount24h: 0,
    });

    await expect(calculator.calculateTokenVolume(138, '0xToken')).resolves.toEqual({
      volume5m: 0,
      volume1h: 0,
      volume24h: 0,
      volume7d: 0,
      volume30d: 0,
      txCount5m: 0,
      txCount1h: 0,
      txCount24h: 0,
    });

    expect(mockLoggerInfo).toHaveBeenCalledTimes(1);
    expect(mockLoggerInfo).toHaveBeenCalledWith(
      'Skipping volume calculation; relation "liquidity_pools" is not available yet',
      expect.objectContaining({
        operation: 'volume calculation',
        relation: 'liquidity_pools',
      })
    );
  });
});
