import { describe, expect, it, vi } from 'vitest';
import {
  getDirectContacts,
  getDirectConversations,
  getDirectThread,
  sendDirectMessage,
} from '@/shared/api/directMessages';
import { apiRequest } from '@/shared/api/httpClient';

vi.mock('@/shared/api/httpClient', () => ({
  apiRequest: vi.fn(),
}));

describe('direct messages API', () => {
  it('loads contacts and conversations', async () => {
    vi.mocked(apiRequest).mockResolvedValue({ rows: [] });

    await getDirectContacts();
    await getDirectConversations();

    expect(apiRequest).toHaveBeenNthCalledWith(1, '/direct_messages/contacts');
    expect(apiRequest).toHaveBeenNthCalledWith(2, '/direct_messages/conversations');
  });

  it('loads a student-scoped thread', async () => {
    vi.mocked(apiRequest).mockResolvedValueOnce({ rows: [], other: null });

    await getDirectThread('user-1', 'student-1');

    expect(apiRequest).toHaveBeenCalledWith('/direct_messages/thread/user-1?studentId=student-1');
  });

  it('sends a message with optional student context', async () => {
    vi.mocked(apiRequest).mockResolvedValueOnce({ id: 'message-1' });

    await sendDirectMessage('user-1', 'Hello', 'student-1');

    expect(apiRequest).toHaveBeenCalledWith('/direct_messages/send', {
      method: 'POST',
      body: { data: { recipientId: 'user-1', body: 'Hello', studentId: 'student-1' } },
    });
  });
});
