'use strict';

const defaultInventarioService = require('../../services/inventarioService');
const {
  validateIngredientCreate,
  validateIngredientUpdate,
} = require('./schemas');

function service(deps = {}) {
  return deps.inventarioService || defaultInventarioService;
}

async function createIngredient(command, _config = {}, deps = {}) {
  const payload = validateIngredientCreate(command.payload || {});
  const result = await service(deps).crearInsumo(payload);
  return {
    ok: true,
    entity_type: 'ingredient',
    entity_id: payload.idinsumo,
    result,
  };
}

async function updateIngredient(command, _config = {}, deps = {}) {
  const payload = validateIngredientUpdate(command.payload || {});
  const { idinsumo, ...changes } = payload;
  const result = await service(deps).actualizarInsumo(idinsumo, changes);
  return {
    ok: true,
    entity_type: 'ingredient',
    entity_id: idinsumo,
    result,
  };
}

module.exports = { createIngredient, updateIngredient };
