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

const { mockGet, mockPost, mockPatch, mockDelete } = vi.hoisted(() => ({
  mockGet: vi.fn(),
  mockPost: vi.fn(),
  mockPatch: vi.fn(),
  mockDelete: vi.fn(),
}));

vi.mock('../../src/services/api-client', () => ({
  apiClient: { get: mockGet, post: mockPost, patch: mockPatch, delete: mockDelete },
}));

import { listStores, getStore, createStore, updateStore, deactivateStore } from '../../src/services/stores';

beforeEach(() => vi.clearAllMocks());

describe('stores service', () => {
  it('listStores calls GET with no query', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listStores('hh1');
    expect(mockGet).toHaveBeenCalledWith('/households/hh1/stores');
  });

  it('listStores builds query string', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listStores('hh1', { tags: 'pharmacy', search: 'cvs', cursor: 'c1', limit: 5 });
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('tags=pharmacy'));
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('search=cvs'));
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('limit=5'));
  });

  it('getStore calls GET with id', async () => {
    mockGet.mockResolvedValue({ _id: 'st-1' });
    await getStore('hh1', 'st-1');
    expect(mockGet).toHaveBeenCalledWith('/households/hh1/stores/st-1');
  });

  it('createStore calls POST', async () => {
    mockPost.mockResolvedValue({ _id: 'st-1' });
    await createStore('hh1', { name: 'CVS' } as never);
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/stores', { name: 'CVS' });
  });

  it('updateStore calls PATCH', async () => {
    mockPatch.mockResolvedValue({ _id: 'st-1' });
    await updateStore('hh1', 'st-1', { name: 'Updated' } as never);
    expect(mockPatch).toHaveBeenCalledWith('/households/hh1/stores/st-1', { name: 'Updated' });
  });

  it('deactivateStore calls DELETE', async () => {
    mockDelete.mockResolvedValue({ _id: 'st-1' });
    await deactivateStore('hh1', 'st-1');
    expect(mockDelete).toHaveBeenCalledWith('/households/hh1/stores/st-1');
  });
});
