import { describe, expect, it } from 'vitest';
import dayjs from 'dayjs';
import { buildSchedulePayload, scheduleToFormValues } from './schedule-form';

describe('schedule edit form mapping', () => {
  it('fills an existing schedule into editable form values', () => {
    const values = scheduleToFormValues({
      id: 3,
      classId: 1,
      classroomId: 1,
      weekDay: 5,
      subject: '语文',
      teacherId: null,
      startTime: '14:00',
      endTime: '18:00',
      startDate: '2026-07-01',
      endDate: '2026-07-31',
      notes: '需要投影设备',
      attendanceAdvanceMinutes: 45,
    });

    expect(values.classroomId).toBe(1);
    expect(values.weekDay).toBe(5);
    expect(values.notes).toBe('需要投影设备');
    expect(values.attendanceAdvanceMinutes).toBe(45);
    expect(values.timeRange.map((item) => item.format('HH:mm'))).toEqual(['14:00', '18:00']);
    expect(values.dateRange.map((item) => item.format('YYYY-MM-DD'))).toEqual([
      '2026-07-01',
      '2026-07-31',
    ]);
  });

  it('builds the update payload from edited form values', () => {
    expect(
      buildSchedulePayload({
        classId: 1,
        classroomId: 2,
        weekDay: 6,
        subject: '作文',
        teacherId: 4,
        timeRange: [dayjs('2026-01-01 13:30'), dayjs('2026-01-01 17:20')],
        dateRange: [dayjs('2026-08-01'), dayjs('2026-08-31')],
        notes: '  临时调整教室  ',
        attendanceAdvanceMinutes: 20,
      }),
    ).toEqual({
      classId: 1,
      classroomId: 2,
      weekDay: 6,
      subject: '作文',
      teacherId: 4,
      startTime: '13:30',
      endTime: '17:20',
      startDate: '2026-08-01',
      endDate: '2026-08-31',
      notes: '临时调整教室',
      attendanceAdvanceMinutes: 20,
    });
  });
});

describe('schedule notes normalization', () => {
  it('omits whitespace-only notes from the payload', () => {
    expect(
      buildSchedulePayload({
        classId: 1,
        classroomId: 2,
        weekDay: 6,
        subject: '作文',
        timeRange: [dayjs('2026-01-01 13:30'), dayjs('2026-01-01 17:20')],
        dateRange: [dayjs('2026-08-01'), dayjs('2026-08-31')],
        notes: '   ',
        attendanceAdvanceMinutes: 30,
      }).notes,
    ).toBeUndefined();
  });
});
