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

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

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

import { listFills, getFill, previewFill, executeFill, undoFill } from '../../src/services/organizer';

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

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

  it('listFills builds query string', async () => {
    mockGet.mockResolvedValue({ data: [] });
    await listFills('hh1', { regimenId: 'reg-1', status: 'active' });
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('regimenId=reg-1'));
    expect(mockGet).toHaveBeenCalledWith(expect.stringContaining('status=active'));
  });

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

  it('getFill calls GET', async () => {
    mockGet.mockResolvedValue({ _id: 'f-1' });
    await getFill('hh1', 'f-1');
    expect(mockGet).toHaveBeenCalledWith('/households/hh1/organizer/fills/f-1');
  });

  it('previewFill calls POST', async () => {
    mockPost.mockResolvedValue({});
    const data = { regimenId: 'reg-1' } as never;
    await previewFill('hh1', data);
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/organizer/preview', data);
  });

  it('executeFill calls POST', async () => {
    mockPost.mockResolvedValue({ _id: 'f-1' });
    const data = { regimenId: 'reg-1' } as never;
    await executeFill('hh1', data);
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/organizer/fill', data);
  });

  it('undoFill calls POST with empty body', async () => {
    mockPost.mockResolvedValue({ _id: 'f-1' });
    await undoFill('hh1', 'f-1');
    expect(mockPost).toHaveBeenCalledWith('/households/hh1/organizer/fills/f-1/undo', {});
  });
});
