import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  listCampusAttendanceConfigs,
  listCampusAttendanceSummaries,
  saveCampusAttendanceConfig,
  saveCampusAttendanceSummary,
} from '@/shared/api/campusAttendance';
import { apiRequest } from '@/shared/api/httpClient';
import type {
  CampusAttendanceConfigMutationDto,
  CampusAttendanceSummaryMutationDto,
} from '@/shared/types/campusAttendance';

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

const apiRequestMock = vi.mocked(apiRequest);

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

  it('lists configs with optional campus filter', () => {
    void listCampusAttendanceConfigs({ campusKey: 'tigers' });

    expect(apiRequestMock).toHaveBeenCalledWith('/campus_attendance/configs?campusKey=tigers');
  });

  it('lists summaries with date and limit filters', () => {
    void listCampusAttendanceSummaries({
      campusKey: 'gators',
      startDate: '2026-06-08',
      endDate: '2026-06-12',
      limit: 10,
    });

    expect(apiRequestMock).toHaveBeenCalledWith(
      '/campus_attendance/summaries?campusKey=gators&startDate=2026-06-08&endDate=2026-06-12&limit=10',
    );
  });

  it('saves campus config through the typed PUT endpoint', () => {
    const request: CampusAttendanceConfigMutationDto = {
      attendance_link: 'https://attendance.example/tigers',
    };

    void saveCampusAttendanceConfig('tigers', request);

    expect(apiRequestMock).toHaveBeenCalledWith('/campus_attendance/configs/tigers', {
      method: 'PUT',
      body: { data: request },
    });
  });

  it('saves campus summary through the typed PUT endpoint', () => {
    const request: CampusAttendanceSummaryMutationDto = {
      total_enrolled: 50,
      total_present: 45,
      total_absent: 4,
      total_tardy: 1,
      notes: 'Two excused absences',
    };

    void saveCampusAttendanceSummary('tigers', '2026-06-08', request);

    expect(apiRequestMock).toHaveBeenCalledWith('/campus_attendance/summaries/tigers/2026-06-08', {
      method: 'PUT',
      body: { data: request },
    });
  });
});
