import { BadRequestException } from '@nestjs/common';
import { SyncController } from './sync.controller';
import { ScheduleSyncQueryDto } from './dto/schedule-sync.dto';

describe('SyncController — schedule sync options', () => {
  it('forwards the attendance-machine-only option', async () => {
    const syncService = {
      syncScheduleToDingTalk: jest.fn().mockResolvedValue({ syncedItems: 0 }),
    };
    const controller = new SyncController(syncService as never);

    await controller.syncSchedule(Object.assign(new ScheduleSyncQueryDto(), {
      dateFrom: '2026-07-10',
      days: 30,
      attendanceMachineOnly: true,
    }));

    expect(syncService.syncScheduleToDingTalk).toHaveBeenCalledWith(
      '2026-07-10',
      30,
      true,
    );
  });

  it.each(['1abc', '0', '-1', '9007199254740992'])(
    'rejects invalid root department id %s',
    async (rootDeptId) => {
      const syncService = { triggerSync: jest.fn() };
      const controller = new SyncController(syncService as never);

      await expect(controller.triggerSync('dingtalk_students', rootDeptId)).rejects.toBeInstanceOf(
        BadRequestException,
      );
      expect(syncService.triggerSync).not.toHaveBeenCalled();
    },
  );

  it('accepts a positive integer root department id', async () => {
    const syncService = { triggerSync: jest.fn().mockResolvedValue([]) };
    const controller = new SyncController(syncService as never);

    await controller.triggerSync('dingtalk_students', '12');

    expect(syncService.triggerSync).toHaveBeenCalledWith('dingtalk_students', 12);
  });

  it('returns Jinshuju form fields for the selector', async () => {
    const syncService = {
      getJinshujuFormFields: jest.fn().mockResolvedValue({
        name: '报名表',
        fields: [{ key: 'field_1', label: '姓名', type: 'single_line_text' }],
      }),
    };
    const controller = new SyncController(syncService as never);

    const result = await controller.getJinshujuFields({
      apiKey: 'key',
      apiSecret: 'secret',
      formToken: 'form-a',
    });

    expect(syncService.getJinshujuFormFields).toHaveBeenCalledWith('key', 'secret', 'form-a');
    expect(result.data.fields[0]).toMatchObject({ key: 'field_1', label: '姓名' });
  });
});
