import QRCode from 'qrcode';
import { v4 as uuidv4 } from 'uuid';
import fs from 'fs';
import { createObjectCsvWriter as createCsvWriter } from 'csv-writer';

// CONFIG
const totalQRCodes = 50;
const baseUrl = 'https://hyperia.so/signup?token='; // Replace with your URL
const outputDir = './qr_codes';

// Ensure output directory exists
if (!fs.existsSync(outputDir)){
    fs.mkdirSync(outputDir);
}

// Create CSV writer
const csvWriter = createCsvWriter({
    path: 'tokens.csv',
    header: [
        {id: 'index', title: 'Index'},
        {id: 'token', title: 'Token'},
        {id: 'url', title: 'URL'},
        {id: 'used', title: 'Used'}
    ]
});

const records = [];

(async () => {
    for (let i = 0; i < totalQRCodes; i++) {
        const token = uuidv4();
        const fullUrl = `${baseUrl}${token}`;
        const fileName = `${outputDir}/qr_${i + 1}_${token}.png`;

        try {
            await QRCode.toFile(fileName, fullUrl);
            console.log(`Generated: ${fileName}`);

            records.push({
                index: i + 1,
                token,
                url: fullUrl,
                used: 'N'
            });
        } catch (err) {
            console.error(`Error generating QR code ${i + 1}:`, err);
        }
    }

    // Save tokens to CSV
    await csvWriter.writeRecords(records);
    console.log('All QR codes generated and tokens saved to tokens.csv');
})();