const customQuery = require("../utils/databaseCustomQueryHelper");
const movimentosService = require("../services/movimentoService");
const { Result } = require("express-validator");

const getStockMaquina = async (maquinaId) => {

    const parameters = {
        intIdMaquina: maquinaId
    }
    const data = await customQuery.executeDynamicQuery("view_Gest_MovimentosMaquina_StockPorPosicao_Nova", parameters, " ORDER BY intSelecao");

    return data;
}

const atualizaStockMaquina = async (maquinaId, userId, data = {}) => {


    // Iterate over each object in the data array
    for (let index = 0; index < data.length; index++) {
        const item = data[index];
        // Extract values from the item object
        
        
        // Construct the parameters for the insert operation
        const insertParameters = {
            intIdMaq: maquinaId,
            intPosicao: item.intSelecao,
            intIdUser: userId,
            intQtd: item.stockAdicionado,
            intIdArtigo: item.intIdArtigo,
            intSinal: 1,
            intIdTamanho: item.intIdTamanho,
            intIdTamanhoArtigo: item.intIdTamanhoArtigo
            // Add more parameters if needed
        };

        // Perform the insert operation
        try {
            // Call your service method to insert data into the database
           const response = await movimentosService.createMovimento(insertParameters);
  
            console.log(`Row ${index} inserted successfully.`);
        } catch (error) {
            console.error(`Error inserting row ${index}:`, error);
            return false;
        }
    };

    return true;
}

module.exports = {
    getStockMaquina,
    atualizaStockMaquina
}