'use strict';

const { createTableExtractor } = require('./extractor-factory');

const movements = createTableExtractor({
  entityType: 'waste_movement',
  table: 'desperdicios',
  idColumns: ['fecha', 'idproducto', 'idinsumo', 'cantidad'],
  watermarkCandidates: ['fecha'],
  columns: ['idproductorelacionado', 'escancelacion', 'esdesperdicio', 'precio', 'costo'],
});

const concepts = createTableExtractor({
  entityType: 'inventory_adjustment_reason',
  table: 'conceptos',
  idColumns: ['idconcepto'],
  columns: ['descripcion', 'tipo', 'autorizacion', 'visible'],
});

function filtered(base, entityType, predicate) {
  return {
    entityType,
    sourceTables: base.sourceTables,
    async extract(pool, cursor, limit) {
      return (await base.extract(pool, cursor, limit)).filter(predicate);
    },
  };
}

const wasteReasons = filtered(concepts, 'waste_reason', (row) => /MERMA|DESPERDICIO/i.test(String(row.descripcion || '')) || ['SPM', 'SPD'].includes(row.idconcepto));
const inventoryAdjustmentReasons = filtered(concepts, 'inventory_adjustment_reason', (row) => /AJUSTE/i.test(String(row.descripcion || '')) || ['EPA', 'SPA'].includes(row.idconcepto));

module.exports = { movements, wasteReasons, inventoryAdjustmentReasons };
