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 {
  listMedicines,
  getMedicine,
  createMedicine,
  updateMedicine,
  deleteMedicine,
  listMedicineProducts,
  createMedicineProduct,
  updateMedicineProduct,
  deleteMedicineProduct,
} from '../../src/services/medicines';

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

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

  it('listMedicines builds query string', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listMedicines('hh1', { q: 'aspirin', category: 'pain', form: 'tablet', limit: 10 });
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('q=aspirin'));
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('category=pain'));
  });

  it('listMedicines with cursor', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listMedicines('hh1', { cursor: 'cur1' });
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('cursor=cur1'));
  });

  it('getMedicine calls GET', async () => {
    mockGet.mockResolvedValue({ _id: 'med-1' });
    await getMedicine('hh1', 'med-1');
    expect(mockGet).toHaveBeenCalledWith('/households/hh1/medicines/med-1');
  });

  it('createMedicine calls POST', async () => {
    mockPost.mockResolvedValue({ _id: 'med-1' });
    await createMedicine('hh1', { name: 'Aspirin' } as never);
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/medicines', { name: 'Aspirin' });
  });

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

  it('deleteMedicine calls DELETE', async () => {
    mockDelete.mockResolvedValue(undefined);
    await deleteMedicine('hh1', 'med-1');
    expect(mockDelete).toHaveBeenCalledWith('/households/hh1/medicines/med-1');
  });

  it('listMedicineProducts builds URL with medicineId', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listMedicineProducts('hh1', 'med-1', { limit: 5 });
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('/medicines/med-1/products'));
  });

  it('listMedicineProducts with cursor', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listMedicineProducts('hh1', 'med-1', { cursor: 'cur1' });
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('cursor=cur1'));
  });

  it('createMedicineProduct calls POST', async () => {
    mockPost.mockResolvedValue({ _id: 'mp-1' });
    await createMedicineProduct('hh1', 'med-1', { brand: 'Bayer' } as never);
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/medicines/med-1/products', {
      brand: 'Bayer',
    });
  });

  it('updateMedicineProduct uses medicine-products path', async () => {
    mockPatch.mockResolvedValue({ _id: 'mp-1' });
    await updateMedicineProduct('hh1', 'mp-1', { brand: 'Updated' } as never);
    expect(mockPatch).toHaveBeenCalledWith('/households/hh1/medicine-products/mp-1', {
      brand: 'Updated',
    });
  });

  it('deleteMedicineProduct calls DELETE', async () => {
    mockDelete.mockResolvedValue(undefined);
    await deleteMedicineProduct('hh1', 'mp-1');
    expect(mockDelete).toHaveBeenCalledWith('/households/hh1/medicine-products/mp-1');
  });
});
