import { PresentismoSyncSchedulerService } from './presentismo-sync-scheduler.service';

describe('PresentismoSyncSchedulerService', () => {
  function createService() {
    const prisma = {
      rrhh_relojes_marcadores: {
        findUnique: jest.fn(),
        findMany: jest.fn(),
      },
    };
    const queue = {
      add: jest.fn().mockResolvedValue(undefined),
      getRepeatableJobs: jest.fn().mockResolvedValue([]),
      removeRepeatableByKey: jest.fn().mockResolvedValue(undefined),
    };

    const service = new PresentismoSyncSchedulerService(prisma as any, queue as any);
    return { service, prisma, queue };
  }

  it('programa job repetible para reloj API activo con intervalo válido', async () => {
    const { service, prisma, queue } = createService();
    prisma.rrhh_relojes_marcadores.findUnique.mockResolvedValue({
      id: 'reloj-1',
      empresa_id: 'emp-1',
      tipo_conexion: 'API',
      activo: true,
      intervalo_polling_min: 10,
    });

    await service.reconciliarReloj('reloj-1');

    expect(queue.add).toHaveBeenCalledWith(
      'sync-reloj-api',
      { relojId: 'reloj-1', empresaId: 'emp-1' },
      expect.objectContaining({
        jobId: 'reloj-sync:reloj-1',
        repeat: { every: 600000 },
      }),
    );
  });

  it('no programa job cuando el reloj no califica (inactivo/no API/sin intervalo)', async () => {
    const { service, prisma, queue } = createService();
    prisma.rrhh_relojes_marcadores.findUnique.mockResolvedValue({
      id: 'reloj-1',
      empresa_id: 'emp-1',
      tipo_conexion: 'PLANILLA',
      activo: true,
      intervalo_polling_min: null,
    });

    await service.reconciliarReloj('reloj-1');
    expect(queue.add).not.toHaveBeenCalled();
  });
});
