const fs = require('fs');
const path = require('path');
const { syncTurn14Inventory } = require('../bulk');

// Path to the JSON file inside 'credentials' folder
const filePath = path.join(__dirname, 'data', 'tokens.json');

// Read and parse the JSON file
const jsonData = JSON.parse(fs.readFileSync(filePath, 'utf8'));

// function logStep(step, message) {
//   console.log(`[${step}] ${message}`);
// }

const logFilePath = path.join(__dirname, "logs", 'BulkInventorySyncJob.log');

function logStep(step, message) {
    const logMessage = `[${new Date().toISOString()}] [${step}] ${message}`;

    // Log to console
    console.log(logMessage);

    // Append to file
    fs.appendFileSync(logFilePath, logMessage + '\n', 'utf8');
}

// Loop through each Shopify store and sync
Object.entries(jsonData).forEach(([shopDomain, details]) => {
    const SHOP = shopDomain.trim();
    const ACCESS_TOKEN = details.accessToken;

    logStep('Bulk Caller Job : ', `Syncing inventory for: ${SHOP}, Access Token : ${ACCESS_TOKEN}`);
    syncTurn14Inventory(SHOP, ACCESS_TOKEN);
});
