import { describe, it, expect, vi, beforeEach } from 'vitest';
import { apiClient } from '../../src/services/api-client';
import * as PricesService from '../../src/services/prices';

vi.mock('../../src/services/api-client', () => ({
  apiClient: {
    get: vi.fn(),
    post: vi.fn(),
  },
}));

describe('prices service', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('recordPrice', async () => {
    const data = { productId: 'p1', price: 10 } as any;
    await PricesService.recordPrice('hh1', data);
    expect(apiClient.post).toHaveBeenCalledWith('/households/hh1/prices', data);
  });

  it('recordBulkPrices', async () => {
    const data = { storeId: 's1', items: [] } as any;
    await PricesService.recordBulkPrices('hh1', data);
    expect(apiClient.post).toHaveBeenCalledWith('/households/hh1/prices/bulk', data);
  });

  it('getPriceHistory with and without query', async () => {
    await PricesService.getPriceHistory('hh1', 'p1');
    expect(apiClient.get).toHaveBeenCalledWith('/households/hh1/prices/history/p1');

    await PricesService.getPriceHistory('hh1', 'p1', {
      storeId: 's1',
      startDate: '2026-05-01',
      endDate: '2026-05-10',
      cursor: 'cur',
      limit: 10,
    });
    expect(apiClient.get).toHaveBeenCalledWith(
      '/households/hh1/prices/history/p1?storeId=s1&startDate=2026-05-01&endDate=2026-05-10&cursor=cur&limit=10'
    );
  });

  it('compareStores', async () => {
    await PricesService.compareStores('hh1', 'p1');
    expect(apiClient.get).toHaveBeenCalledWith('/households/hh1/prices/compare/p1');
  });

  it('getPriceAnalytics', async () => {
    await PricesService.getPriceAnalytics('hh1');
    expect(apiClient.get).toHaveBeenCalledWith('/households/hh1/prices/analytics');
  });
});
