import 'reflect-metadata';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { ScheduleSyncQueryDto } from './schedule-sync.dto';

describe('ScheduleSyncQueryDto', () => {
  it('transforms valid query strings', async () => {
    const dto = plainToInstance(ScheduleSyncQueryDto, {
      dateFrom: '2026-07-13',
      days: '7',
      attendanceMachineOnly: 'true',
    });
    expect(await validate(dto)).toEqual([]);
    expect(dto).toMatchObject({ days: 7, attendanceMachineOnly: true });
  });

  it.each(['0', '-1', '91', '7.5', 'abc'])('rejects invalid sync days %s', async (days) => {
    const dto = plainToInstance(ScheduleSyncQueryDto, { days });
    expect((await validate(dto)).some((error) => error.property === 'days')).toBe(true);
  });

  it.each(['not-a-date', '2026-02-31', '2026-07-13T00:00:00Z'])(
    'rejects invalid or non-date-only start date %s',
    async (dateFrom) => {
      const dto = plainToInstance(ScheduleSyncQueryDto, { dateFrom });
      expect((await validate(dto)).some((error) => error.property === 'dateFrom')).toBe(true);
    },
  );

  it('treats non-true boolean strings as false', async () => {
    const dto = plainToInstance(ScheduleSyncQueryDto, { attendanceMachineOnly: 'false' });
    expect(await validate(dto)).toEqual([]);
    expect(dto.attendanceMachineOnly).toBe(false);
  });
});
