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 {
  listRegimens,
  getRegimen,
  getBurnRates,
  createRegimen,
  updateRegimen,
  deleteRegimen,
} from '../../src/services/regimens';

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

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

  it('listRegimens builds query string with isActive', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listRegimens('hh1', { isActive: true, limit: 10 });
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('isActive=true'));
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('limit=10'));
  });

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

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

  it('getBurnRates calls GET', async () => {
    mockGet.mockResolvedValue({});
    await getBurnRates('hh1');
    expect(mockGet).toHaveBeenCalledWith('/households/hh1/regimens/burn-rate');
  });

  it('createRegimen calls POST', async () => {
    mockPost.mockResolvedValue({ _id: 'reg-1' });
    const data = { name: 'Morning' } as never;
    await createRegimen('hh1', data);
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/regimens', data);
  });

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

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