import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  checkInZone,
  clearTodayZoneCheckin,
  getZoneCheckinCompletion,
  getTodayZoneCheckin,
  listZoneCheckinHistory,
} from '@/shared/api/zoneCheckins';
import { apiRequest } from '@/shared/api/httpClient';
import type { ZoneCheckinTodayDto } from '@/shared/types/zoneCheckins';

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

const apiRequestMock = vi.mocked(apiRequest);

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

  it('fetches today zone check-in status', async () => {
    const todayCheckin: ZoneCheckinTodayDto = {
      date: '2026-06-12',
      zone: 'green',
      isCheckedInToday: true,
    };
    apiRequestMock.mockResolvedValueOnce(todayCheckin);

    await expect(getTodayZoneCheckin()).resolves.toEqual(todayCheckin);

    expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins/today');
  });

  it('checks in to a zone', async () => {
    const todayCheckin: ZoneCheckinTodayDto = {
      date: '2026-06-12',
      zone: 'blue',
      isCheckedInToday: true,
    };
    apiRequestMock.mockResolvedValueOnce(todayCheckin);

    await expect(checkInZone('blue')).resolves.toEqual(todayCheckin);

    expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins', {
      method: 'POST',
      body: { data: { zone: 'blue' } },
    });
  });

  it('clears today zone check-in', async () => {
    const clearedCheckin: ZoneCheckinTodayDto = {
      date: '2026-06-12',
      zone: null,
      isCheckedInToday: false,
    };
    apiRequestMock.mockResolvedValueOnce(clearedCheckin);

    await expect(clearTodayZoneCheckin()).resolves.toEqual(clearedCheckin);

    expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins/today', {
      method: 'DELETE',
    });
  });

  it('lists zone check-in history', async () => {
    const historyResponse = {
      rows: [
        { id: '1', zone: 'green', date: '2026-06-11' },
        { id: '2', zone: 'yellow', date: '2026-06-10' },
      ],
      count: 2,
    };
    apiRequestMock.mockResolvedValueOnce(historyResponse);

    await expect(listZoneCheckinHistory()).resolves.toEqual(historyResponse);

    expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins');
  });

  it('fetches scoped zone check-in completion', async () => {
    const completion = {
      summary: {
        totalStaff: 2,
        completedCount: 1,
        pendingCount: 1,
        greenCount: 0,
        nonGreenCount: 1,
        completionRate: 50,
      },
      rows: [
        {
          userId: 'user-1',
          name: 'Ava Lee',
          email: 'ava@example.test',
          role: 'teacher',
          date: '2026-06-18',
          status: 'complete',
          zone: 'yellow',
          riskLevel: 'medium',
          result: 'Yellow Zone',
        },
      ],
    } as const;
    apiRequestMock.mockResolvedValueOnce(completion);

    await expect(getZoneCheckinCompletion()).resolves.toEqual(completion);

    expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins/completion');
  });
});
