const { getBlockingProfilePickup } = require('../../services/profileCheckTolerance');

const NOW = new Date('2026-07-16T10:00:00+02:00');

function pickup(date) {
  return { date };
}

describe('pickup profile check tolerance', () => {
  test('blocks today profile pickup for regular stores', () => {
    const blocking = getBlockingProfilePickup({
      storeId: '44975',
      profilePickups: [pickup('2026-07-16T17:00:00+02:00')],
      targetPickup: pickup('2026-07-17T17:00:00+02:00'),
      now: NOW
    });

    expect(blocking).toEqual(pickup('2026-07-16T17:00:00+02:00'));
  });

  test('allows future target for tolerated store when profile pickup is today', () => {
    const blocking = getBlockingProfilePickup({
      storeId: '33875',
      profilePickups: [pickup('2026-07-16T17:00:00+02:00')],
      targetPickup: pickup('2026-07-17T17:00:00+02:00'),
      now: NOW
    });

    expect(blocking).toBeNull();
  });

  test('blocks same-day target for tolerated store', () => {
    const blocking = getBlockingProfilePickup({
      storeId: '33875',
      profilePickups: [pickup('2026-07-16T12:00:00+02:00')],
      targetPickup: pickup('2026-07-16T18:00:00+02:00'),
      now: NOW
    });

    expect(blocking).toEqual(pickup('2026-07-16T12:00:00+02:00'));
  });

  test('blocks non-today profile pickups for tolerated store', () => {
    const blocking = getBlockingProfilePickup({
      storeId: '33875',
      profilePickups: [pickup('2026-07-17T17:00:00+02:00')],
      targetPickup: pickup('2026-07-18T17:00:00+02:00'),
      now: NOW
    });

    expect(blocking).toEqual(pickup('2026-07-17T17:00:00+02:00'));
  });

  test('blocks when any profile pickup is outside the tolerated day', () => {
    const blocking = getBlockingProfilePickup({
      storeId: '33875',
      profilePickups: [
        pickup('2026-07-16T17:00:00+02:00'),
        pickup('2026-07-18T17:00:00+02:00')
      ],
      targetPickup: pickup('2026-07-19T17:00:00+02:00'),
      now: NOW
    });

    expect(blocking).toEqual(pickup('2026-07-18T17:00:00+02:00'));
  });
});
