import { CLAVES_RRHH, REGLAS_ALERTA, armarAlertaRegla, fmtGs } from './reglas-catalogo';

describe('reglas-catalogo', () => {
  it('cada regla tiene módulo, privilegio y ruta', () => {
    for (const [clave, r] of Object.entries(REGLAS_ALERTA)) {
      expect(r.clave).toBe(clave);
      expect(r.modulo).toMatch(/^(COBRANZAS|INVENTARIO|TESORERIA|RRHH)$/);
      expect(r.privilegio).toMatch(/_VER$/);
      expect(r.ruta.startsWith('/')).toBe(true);
    }
    expect(Object.keys(REGLAS_ALERTA)).toHaveLength(10);
    expect(CLAVES_RRHH).toEqual(Object.keys(REGLAS_ALERTA).filter((c) => c.startsWith('rrhh_')));
  });

  it('armarAlertaRegla completa módulo/privilegio/ruta/origen y recorta items a 5', () => {
    const items = Array.from({ length: 7 }, (_, i) => ({ id: `p${i}`, nombre: `Prod ${i}`, valor: i, detalle: `${i} u.` }));
    const a = armarAlertaRegla({
      empresaId: 'emp-1', clave: 'stock_bajo', criticidad: 'alta',
      titulo: '7 productos con stock bajo', descripcion: 'x', cantidad: 7, items,
    });
    expect(a).toMatchObject({
      empresaId: 'emp-1', clave: 'stock_bajo', tipo: 'stock_bajo', origen: 'regla',
      modulo: 'INVENTARIO', privilegioRequerido: 'INV_STK_STOCK_VER', ruta: '/reportes/inventario/stock',
      accionSugerida: REGLAS_ALERTA.stock_bajo.accionSugerida, sucursalId: null, entidadTipo: null, entidadId: null,
    });
    expect((a.datos as any).cantidad).toBe(7);
    expect((a.datos as any).items).toHaveLength(5);
    expect((a.datos as any).total).toBeNull();
  });

  it('fmtGs redondea y usa separador de miles', () => {
    expect(fmtGs(1234567.6)).toBe(`Gs. ${(1234568).toLocaleString('es-PY')}`);
  });
});
