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

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

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

import {
  createHousehold,
  getHousehold,
  updateHousehold,
  generateInviteCode,
  joinHousehold,
} from '../../src/services/households';

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

describe('households service', () => {
  it('createHousehold calls POST', async () => {
    mockPost.mockResolvedValue({ _id: 'hh1' });
    await createHousehold('My Home');
    expect(mockPost).toHaveBeenCalledWith('/households', { name: 'My Home' });
  });

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

  it('updateHousehold calls PATCH', async () => {
    mockPatch.mockResolvedValue({ _id: 'hh1' });
    await updateHousehold('hh1', { name: 'Updated' });
    expect(mockPatch).toHaveBeenCalledWith('/households/hh1', { name: 'Updated' });
  });

  it('generateInviteCode calls POST', async () => {
    mockPost.mockResolvedValue({ _id: 'hh1' });
    await generateInviteCode('hh1');
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/invite');
  });

  it('joinHousehold calls POST with invite code', async () => {
    mockPost.mockResolvedValue({ _id: 'hh1' });
    await joinHousehold('ABC123');
    expect(mockPost).toHaveBeenCalledWith('/households/join', { inviteCode: 'ABC123' });
  });
});
