import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  cancelCommunicationEvent,
  createCommunicationEvent,
  deleteCommunicationEvent,
  listCommunicationEvents,
  updateCommunicationEvent,
} from '@/shared/api/communications';
import { apiRequest } from '@/shared/api/httpClient';
import type { CommunicationEventCreateDto } 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 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',
      targets: [{ level: 'campus', id: 'campus-1' }],
    };

    void createCommunicationEvent(request);

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

  it('updates communication events with PATCH body wrapped in data', () => {
    void updateCommunicationEvent({
      id: 'event-1',
      title: 'Updated alert',
      date: '2026-06-10',
    });

    expect(apiRequestMock).toHaveBeenCalledWith('/communications/events/event-1', {
      method: 'PATCH',
      body: { data: { title: 'Updated alert', date: '2026-06-10' } },
    });
  });

  it('deletes communication events', () => {
    void deleteCommunicationEvent('event-1');

    expect(apiRequestMock).toHaveBeenCalledWith('/communications/events/event-1', {
      method: 'DELETE',
    });
  });

  it('cancels communication events with POST body wrapped in data', () => {
    void cancelCommunicationEvent({ id: 'event-1', reason: 'Weather' });

    expect(apiRequestMock).toHaveBeenCalledWith('/communications/events/event-1/cancel', {
      method: 'POST',
      body: { data: { reason: 'Weather' } },
    });
  });
});
