import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  createCommunicationEvent,
  createParentMessage,
  listCommunicationEvents,
  listParentMessages,
} from '@/shared/api/communications';
import { apiRequest } from '@/shared/api/httpClient';
import type {
  CommunicationEventCreateDto,
  ParentMessageCreateDto,
} from '@/shared/types/communications';

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

const apiRequestMock = vi.mocked(apiRequest);

describe('communications API', () => {
  beforeEach(() => {
    apiRequestMock.mockReset();
  });

  it('lists parent messages with optional category filtering', () => {
    void listParentMessages('progress');

    expect(apiRequestMock).toHaveBeenCalledWith('/communications/parent-messages?category=progress');
  });

  it('creates parent messages with POST body wrapped in data', () => {
    const request: ParentMessageCreateDto = {
      recipientName: 'Family Contact',
      messageText: 'Student had a strong morning.',
      category: 'progress',
    };

    void createParentMessage(request);

    expect(apiRequestMock).toHaveBeenCalledWith('/communications/parent-messages', {
      method: 'POST',
      body: { data: request },
    });
  });

  it('lists communication events with optional type filtering', () => {
    void listCommunicationEvents('drill');

    expect(apiRequestMock).toHaveBeenCalledWith('/communications/events?type=drill');
  });

  it('creates communication events with POST body wrapped in data', () => {
    const request: CommunicationEventCreateDto = {
      title: 'Safety drill',
      date: '2026-06-08',
      type: 'drill',
      roles: ['teacher', 'support_staff', 'director'],
    };

    void createCommunicationEvent(request);

    expect(apiRequestMock).toHaveBeenCalledWith('/communications/events', {
      method: 'POST',
      body: { data: request },
    });
  });
});
