import { cambiaElPago, esEdicionFinanciera, GastoComparable, normalizarFormaPago } from './edicion-financiera';

const PYG = 'mon-pyg';
const USD = 'mon-usd';

const existente = (extra: Partial<GastoComparable> = {}): GastoComparable => ({
  proveedor_id: 'prov-1',
  forma_pago: 'CONTADO',
  requisicion_id: null,
  moneda_id: PYG,
  cotizacion: '1.000000',
  cuenta_tesoreria_id: 'cta-1',
  medio_pago_id: 'mp-1',
  total: '110000',
  gasto_det: [
    {
      producto_id: null,
      tipo_gasto_id: 'tg-1',
      descripcion: 'Combustible',
      cantidad: '1.0000',
      precio_unitario: '110000.0000',
      porcentaje_iva: '10.00',
    },
  ],
  ...extra,
});

const item = (extra: Record<string, unknown> = {}) => ({
  tipo_gasto_id: 'tg-1',
  descripcion: 'Combustible',
  cantidad: 1,
  precio_unitario: 110000,
  porcentaje_iva: 10,
  ...extra,
});

describe('normalizarFormaPago', () => {
  it('TRANSFERENCIA y vacío son CONTADO', () => {
    expect(normalizarFormaPago('TRANSFERENCIA')).toBe('CONTADO');
    expect(normalizarFormaPago(undefined)).toBe('CONTADO');
    expect(normalizarFormaPago(null)).toBe('CONTADO');
    expect(normalizarFormaPago('')).toBe('CONTADO');
    expect(normalizarFormaPago('credito')).toBe('CREDITO');
    expect(normalizarFormaPago('ANTICIPO')).toBe('ANTICIPO');
  });
});

describe('esEdicionFinanciera', () => {
  it('mismo proveedor, forma e ítems → false', () => {
    const dto = {
      cabecera: {
        proveedor_id: 'prov-1',
        forma_pago: 'CONTADO' as const,
        moneda_id: PYG,
        cotizacion: 1,
        requisicion_id: undefined,
      },
      items: [item()],
    };
    expect(esEdicionFinanciera(existente(), dto, PYG)).toBe(false);
  });

  it('lo que no viene en el DTO no cambió', () => {
    expect(esEdicionFinanciera(existente(), { cabecera: { descripcion: 'otra' } }, PYG)).toBe(false);
    expect(esEdicionFinanciera(existente(), {}, PYG)).toBe(false);
  });

  it('TRANSFERENCIA vs CONTADO → false', () => {
    expect(
      esEdicionFinanciera(existente({ forma_pago: 'TRANSFERENCIA' }), { cabecera: { forma_pago: 'CONTADO' } }, PYG),
    ).toBe(false);
    expect(esEdicionFinanciera(existente(), { cabecera: { forma_pago: 'TRANSFERENCIA' } }, PYG)).toBe(false);
  });

  it('CONTADO → CREDITO → true', () => {
    expect(esEdicionFinanciera(existente(), { cabecera: { forma_pago: 'CREDITO' } }, PYG)).toBe(true);
  });

  it('moneda NULL vs PYG → false', () => {
    expect(
      esEdicionFinanciera(existente({ moneda_id: null }), { cabecera: { moneda_id: PYG, cotizacion: 1 } }, PYG),
    ).toBe(false);
  });

  it('PYG → USD → true', () => {
    expect(esEdicionFinanciera(existente(), { cabecera: { moneda_id: USD, cotizacion: 7300 } }, PYG)).toBe(true);
  });

  it("cotización 7300 vs '7300.000000' → false; 7300 → 7350 → true", () => {
    const usd = existente({ moneda_id: USD, cotizacion: '7300.000000' });
    expect(esEdicionFinanciera(usd, { cabecera: { moneda_id: USD, cotizacion: 7300 } }, PYG)).toBe(false);
    expect(esEdicionFinanciera(usd, { cabecera: { moneda_id: USD, cotizacion: 7350 } }, PYG)).toBe(true);
  });

  it('en guaraníes la cotización que venga no cuenta', () => {
    expect(
      esEdicionFinanciera(existente({ moneda_id: null, cotizacion: '1' }), { cabecera: { cotizacion: 7300 } }, PYG),
    ).toBe(false);
  });

  it('precio 100 → 110 → true', () => {
    const ex = existente({
      gasto_det: [
        {
          tipo_gasto_id: 'tg-1',
          descripcion: 'Combustible',
          cantidad: '1.0000',
          precio_unitario: '100.0000',
          porcentaje_iva: '10.00',
        },
      ],
    });
    expect(esEdicionFinanciera(ex, { items: [item({ precio_unitario: 100 })] }, PYG)).toBe(false);
    expect(esEdicionFinanciera(ex, { items: [item({ precio_unitario: 110 })] }, PYG)).toBe(true);
  });

  it('cambia descripción, tipo, cantidad, IVA o la cantidad de ítems → true', () => {
    expect(esEdicionFinanciera(existente(), { items: [item({ descripcion: 'Peaje' })] }, PYG)).toBe(true);
    expect(esEdicionFinanciera(existente(), { items: [item({ tipo_gasto_id: 'tg-2' })] }, PYG)).toBe(true);
    expect(esEdicionFinanciera(existente(), { items: [item({ cantidad: 2 })] }, PYG)).toBe(true);
    expect(esEdicionFinanciera(existente(), { items: [item({ porcentaje_iva: 5 })] }, PYG)).toBe(true);
    expect(esEdicionFinanciera(existente(), { items: [item(), item()] }, PYG)).toBe(true);
  });

  it('mover un ítem a otra línea de la requisición → true', () => {
    expect(esEdicionFinanciera(existente(), { items: [item({ requisicion_det_id: 'rd-2' })] }, PYG)).toBe(true);
    expect(esEdicionFinanciera(existente(), { items: [item({ requisicion_det_id: null })] }, PYG)).toBe(false);
  });

  it('cambia proveedor o requisición → true', () => {
    expect(esEdicionFinanciera(existente(), { cabecera: { proveedor_id: 'prov-2' } }, PYG)).toBe(true);
    expect(esEdicionFinanciera(existente(), { cabecera: { requisicion_id: 'req-1' } }, PYG)).toBe(true);
  });
});

describe('cambiaElPago', () => {
  it('sin cambios de moneda, cotización, cuenta, medio ni total → false', () => {
    const dto = {
      cabecera: {
        moneda_id: PYG,
        cotizacion: 1,
        cuenta_tesoreria_id: 'cta-1',
        medio_pago_id: 'mp-1',
        fecha_vencimiento: '2026-11-01',
      },
      items: [item()],
    };
    expect(cambiaElPago(existente(), dto, PYG)).toBe(false);
  });

  it('cambia la cuenta, el medio o el total → true', () => {
    expect(cambiaElPago(existente(), { cabecera: { cuenta_tesoreria_id: 'cta-2' } }, PYG)).toBe(true);
    expect(cambiaElPago(existente(), { cabecera: { medio_pago_id: 'mp-2' } }, PYG)).toBe(true);
    expect(cambiaElPago(existente(), { items: [item({ precio_unitario: 120000 })] }, PYG)).toBe(true);
  });

  it('cambia la moneda o la cotización → true', () => {
    expect(cambiaElPago(existente(), { cabecera: { moneda_id: USD, cotizacion: 7300 } }, PYG)).toBe(true);
    expect(
      cambiaElPago(existente({ moneda_id: USD, cotizacion: '7300.000000' }), { cabecera: { cotizacion: 7400 } }, PYG),
    ).toBe(true);
  });

  it('una descripción distinta del ítem con el mismo total no cambia el pago', () => {
    expect(cambiaElPago(existente(), { items: [item({ descripcion: 'Nafta' })] }, PYG)).toBe(false);
  });
});
