import { describe, expect, it } from 'vitest';
import {
  buildAttendanceEntryInput,
  buildOverallAttendanceStats,
  getWeekEnd,
  getWeekStart,
} from '@/business/campus-attendance/selectors';
import type {
  CampusAttendanceEntryDraft,
  CampusAttendanceSummaryViewModel,
} from '@/business/campus-attendance/types';

const summaries: readonly CampusAttendanceSummaryViewModel[] = [
  {
    id: 'summary-1',
    campus_id: 'tigers',
    date: '2026-06-08',
    total_enrolled: 100,
    total_present: 90,
    total_absent: 10,
    total_tardy: 3,
    attendance_percentage: 90,
    recorded_by: 'director-1',
    notes: null,
  },
  {
    id: 'summary-2',
    campus_id: 'gators',
    date: '2026-06-08',
    total_enrolled: 50,
    total_present: 40,
    total_absent: 10,
    total_tardy: 1,
    attendance_percentage: 80,
    recorded_by: 'director-1',
    notes: null,
  },
  {
    id: 'summary-3',
    campus_id: 'tigers',
    date: '2026-06-09',
    total_enrolled: 100,
    total_present: 95,
    total_absent: 5,
    total_tardy: 2,
    attendance_percentage: 95,
    recorded_by: 'director-1',
    notes: null,
  },
];

describe('campus attendance selectors', () => {
  it('calculates Monday and Friday boundaries for a midweek date', () => {
    const date = new Date('2026-06-10T12:00:00Z');

    expect(getWeekStart(date)).toBe('2026-06-08');
    expect(getWeekEnd(date)).toBe('2026-06-12');
  });

  it('builds validated attendance input and normalizes optional notes', () => {
    const draft: CampusAttendanceEntryDraft = {
      date: '2026-06-08',
      enrolled: '25',
      present: '21',
      absent: '4',
      tardy: '',
      notes: '  ',
    };

    expect(buildAttendanceEntryInput(draft, 'tigers')).toEqual({
      campusId: 'tigers',
      date: '2026-06-08',
      totalEnrolled: 25,
      totalPresent: 21,
      totalAbsent: 4,
      totalTardy: 0,
      notes: null,
    });
  });

  it('rejects attendance input without a positive enrollment count', () => {
    const draft: CampusAttendanceEntryDraft = {
      date: '2026-06-08',
      enrolled: '0',
      present: '0',
      absent: '0',
      tardy: '0',
      notes: 'Closed',
    };

    expect(buildAttendanceEntryInput(draft, 'tigers')).toBeNull();
  });

  it('aggregates daily and weekly attendance percentages across campuses', () => {
    expect(
      buildOverallAttendanceStats(summaries, '2026-06-08', '2026-06-08', '2026-06-12'),
    ).toMatchObject({
      todayEnrolled: 150,
      todayPresent: 130,
      todayPct: 86.67,
      weekPct: 90.83,
    });
  });
});
