jest.mock('src/constants/env', () => ({
  isStaging: false,
  QA_CUSTOM_DEV_INSTANCE_ID: 496,
}));

import {
  isPanalabSpecialForm,
  isPanalabSpecialLimitForm,
  isTestingInstance,
  isViaticumAmountField,
  isViaticumFormTotalAmount,
  isViaticumTotalAmountField,
} from './utils';

const PANALAB_INSTANCE_ID = 9128;
const OTHER_INSTANCE_ID = 1234;

describe('isTestingInstance', () => {
  it('returns false when not in staging environment', () => {
    expect(isTestingInstance(496)).toBe(false);
    expect(isTestingInstance(PANALAB_INSTANCE_ID)).toBe(false);
  });
});

describe('isPanalabSpecialForm', () => {
  it('returns true for any Panalab special form title on the Panalab instance', () => {
    expect(
      isPanalabSpecialForm(PANALAB_INSTANCE_ID, 'Viáticos (Gastos Comunes)'),
    ).toBe(true);
    expect(isPanalabSpecialForm(PANALAB_INSTANCE_ID, 'Viáticos Staff')).toBe(
      true,
    );
    expect(isPanalabSpecialForm(PANALAB_INSTANCE_ID, 'Viáticos APM')).toBe(
      true,
    );
    expect(isPanalabSpecialForm(PANALAB_INSTANCE_ID, 'Viáticos APF')).toBe(
      true,
    );
    expect(isPanalabSpecialForm(PANALAB_INSTANCE_ID, 'Viáticos')).toBe(true);
    expect(
      isPanalabSpecialForm(
        PANALAB_INSTANCE_ID,
        'Viáticos (Gastos Giras, Promocionales, Congregos)',
      ),
    ).toBe(true);
  });

  it('returns false for unrecognised form titles on the Panalab instance', () => {
    expect(isPanalabSpecialForm(PANALAB_INSTANCE_ID, 'Some other form')).toBe(
      false,
    );
    expect(isPanalabSpecialForm(PANALAB_INSTANCE_ID, '')).toBe(false);
  });

  it('returns false for Panalab form titles on non-Panalab instances', () => {
    expect(
      isPanalabSpecialForm(OTHER_INSTANCE_ID, 'Viáticos (Gastos Comunes)'),
    ).toBe(false);
  });
});

describe('isPanalabSpecialLimitForm', () => {
  it('returns true only for the limit-form titles on the Panalab instance', () => {
    expect(
      isPanalabSpecialLimitForm(
        PANALAB_INSTANCE_ID,
        'Viáticos (Gastos Comunes)',
      ),
    ).toBe(true);
    expect(
      isPanalabSpecialLimitForm(PANALAB_INSTANCE_ID, 'Viáticos Staff'),
    ).toBe(true);
    expect(isPanalabSpecialLimitForm(PANALAB_INSTANCE_ID, 'Viáticos APM')).toBe(
      true,
    );
    expect(isPanalabSpecialLimitForm(PANALAB_INSTANCE_ID, 'Viáticos APF')).toBe(
      true,
    );
  });

  it('returns false for the non-limit Panalab forms', () => {
    expect(isPanalabSpecialLimitForm(PANALAB_INSTANCE_ID, 'Viáticos')).toBe(
      false,
    );
    expect(
      isPanalabSpecialLimitForm(
        PANALAB_INSTANCE_ID,
        'Viáticos (Gastos Giras, Promocionales, Congregos)',
      ),
    ).toBe(false);
  });

  it('returns false on non-Panalab instances', () => {
    expect(
      isPanalabSpecialLimitForm(OTHER_INSTANCE_ID, 'Viáticos (Gastos Comunes)'),
    ).toBe(false);
  });
});

describe('isViaticumAmountField', () => {
  it('returns true for descriptions starting with "Monto de" on the Panalab instance', () => {
    expect(
      isViaticumAmountField('Monto de Combustible', PANALAB_INSTANCE_ID),
    ).toBe(true);
    expect(
      isViaticumAmountField(
        'Monto de Combustible - Carga 1',
        PANALAB_INSTANCE_ID,
      ),
    ).toBe(true);
    expect(
      isViaticumAmountField(
        'Monto de Almuerzos/Cena - Comida del lunes',
        PANALAB_INSTANCE_ID,
      ),
    ).toBe(true);
  });

  it('returns false for "Monto Total de" descriptions (different prefix)', () => {
    expect(
      isViaticumAmountField('Monto Total de Combustible', PANALAB_INSTANCE_ID),
    ).toBe(false);
    expect(
      isViaticumAmountField('Monto Total de Viáticos', PANALAB_INSTANCE_ID),
    ).toBe(false);
  });

  it('returns false for unrelated descriptions', () => {
    expect(isViaticumAmountField('', PANALAB_INSTANCE_ID)).toBe(false);
    expect(isViaticumAmountField('Combustible', PANALAB_INSTANCE_ID)).toBe(
      false,
    );
    expect(
      isViaticumAmountField('Total: Monto de algo', PANALAB_INSTANCE_ID),
    ).toBe(false);
  });

  it('returns false on non-Panalab instances even with matching prefix', () => {
    expect(
      isViaticumAmountField('Monto de Combustible', OTHER_INSTANCE_ID),
    ).toBe(false);
  });
});

describe('isViaticumTotalAmountField', () => {
  it('returns true for descriptions starting with "Monto Total de" on the Panalab instance', () => {
    expect(
      isViaticumTotalAmountField(
        'Monto Total de Combustible',
        PANALAB_INSTANCE_ID,
      ),
    ).toBe(true);
    expect(
      isViaticumTotalAmountField(
        'Monto Total de Viáticos',
        PANALAB_INSTANCE_ID,
      ),
    ).toBe(true);
  });

  it('returns false for amount-field descriptions ("Monto de")', () => {
    expect(
      isViaticumTotalAmountField('Monto de Combustible', PANALAB_INSTANCE_ID),
    ).toBe(false);
  });

  it('returns false on non-Panalab instances even with matching prefix', () => {
    expect(
      isViaticumTotalAmountField(
        'Monto Total de Combustible',
        OTHER_INSTANCE_ID,
      ),
    ).toBe(false);
  });
});

describe('isViaticumFormTotalAmount', () => {
  it('returns true only for the exact "Monto Total de Viáticos" description', () => {
    expect(isViaticumFormTotalAmount('Monto Total de Viáticos')).toBe(true);
  });

  it('returns false for any other description (instance-agnostic)', () => {
    expect(isViaticumFormTotalAmount('Monto Total de Combustible')).toBe(false);
    expect(isViaticumFormTotalAmount('Monto de Combustible')).toBe(false);
    expect(isViaticumFormTotalAmount('')).toBe(false);
    expect(isViaticumFormTotalAmount('monto total de viáticos')).toBe(false);
  });
});
