import { describe, expect, it } from 'vitest';
import {
  buildStaffAttendanceRollups,
  countStaffAttendanceStatus,
  recentStaffAttendanceRecords,
  staffAttendanceRate,
} from '@/business/staff-attendance/selectors';
import type { StaffAttendanceRecordViewModel } from '@/business/staff-attendance/types';

const records: readonly StaffAttendanceRecordViewModel[] = [
  { id: '1', date: '2026-06-08', status: 'present', note: null, userName: 'Ava Lee', userRole: 'teacher' },
  { id: '2', date: '2026-06-08', status: 'late', note: null, userName: 'Ava Lee', userRole: 'teacher' },
  { id: '3', date: '2026-06-08', status: 'absent', note: null, userName: 'Ben Kim', userRole: 'support_staff' },
  { id: '4', date: '2026-06-09', status: 'absent', note: null, userName: 'Ben Kim', userRole: 'support_staff' },
  { id: '5', date: '2026-06-10', status: 'absent', note: null, userName: 'Ben Kim', userRole: 'support_staff' },
];

describe('staff attendance selectors', () => {
  it('counts records by attendance status', () => {
    expect(countStaffAttendanceStatus(records, 'present')).toBe(1);
    expect(countStaffAttendanceStatus(records, 'late')).toBe(1);
    expect(countStaffAttendanceStatus(records, 'absent')).toBe(3);
  });

  it('calculates present attendance rate from the whole record set', () => {
    expect(staffAttendanceRate(records)).toBe(20);
    expect(staffAttendanceRate([])).toBe(0);
  });

  it('keeps recent records in source order with a hard limit', () => {
    expect(recentStaffAttendanceRecords(records, 2)).toEqual(records.slice(0, 2));
  });

  it('builds per-staff rollups and marks repeated absences as a downward trend', () => {
    expect(buildStaffAttendanceRollups(records)).toEqual([
      { name: 'Ava Lee', present: 1, late: 1, absent: 0, trend: 'up' },
      { name: 'Ben Kim', present: 0, late: 0, absent: 3, trend: 'down' },
    ]);
  });
});
