const fs = require('fs');
const path = require('path');
const crypto = require('crypto');

const INPUT_FILE = path.join(__dirname, '../firebase/internal-launchpad/iap-users.txt');
const OUTPUT_FILE = path.join(__dirname, '../firebase/internal-launchpad/allowed-hashes.json');

try {
    const data = fs.readFileSync(INPUT_FILE, 'utf8');
    const lines = data.split('\n');

    const hashes = lines
        .map(line => line.trim())
        .filter(line => line && !line.startsWith('#')) // Ignore empty lines and comments
        .map(line => line.replace(/^user:/, '').trim().toLowerCase()) // Clean email
        .map(email => {
            // Create SHA-256 hash
            return crypto.createHash('sha256').update(email).digest('hex');
        });

    const jsonContent = JSON.stringify(hashes, null, 2);
    fs.writeFileSync(OUTPUT_FILE, jsonContent);

    console.log(`✅ Successfully generated ${hashes.length} secure hashes from iap-users.txt`);
    console.log(`   Output: ${OUTPUT_FILE}`);

} catch (err) {
    console.error('❌ Error generating hashes:', err);
    process.exit(1);
}

