const { fetchAllSets } = require('../services/ygoproService');
const { upsertSet } = require('../models/setModel');

const BATCH_SIZE = 10;

async function importSets(req, res) {
  try {
    const sets = await fetchAllSets();
    let added = 0;

    // Parallelized batch insert
    for (let i = 0; i < sets.length; i += BATCH_SIZE) {
      const batch = sets.slice(i, i + BATCH_SIZE);
      await Promise.all(batch.map(async (set) => {
        await upsertSet(set);
        added++;
      }));
    }

    res.json({
      added,
      total: sets.length
    });
  } catch (err) {
    console.error('Error importing sets:', err);
    res.status(500).json({ error: 'Failed to import sets' });
  }
}

module.exports = { importSets };