// gen_license.js
// Utility to generate a valid hardware-locked license key for REDROOM
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');

// Load environment variables if they exist
let envPath = path.join(__dirname, '.env');
if (!fs.existsSync(envPath)) {
  envPath = path.join(__dirname, '..', '.env');
}
if (fs.existsSync(envPath)) {
  require('dotenv').config({ path: envPath });
}

const secret = process.env.TRUSTED_LICENSE_KEY || 'REDROOM_SECRET_KEY_2026';
const targetMachineId = process.argv[2];

if (!targetMachineId) {
  console.log('\n❌ ERROR: Debes proporcionar el Machine ID del cliente.');
  console.log('Uso: node gen_license.js <MACHINE_ID>');
  console.log('Ejemplo: node gen_license.js a8f3-8b92-9c12-32a8\n');
  process.exit(1);
}

const cleanId = targetMachineId.trim();
const licenseKey = crypto.createHmac('sha256', secret)
                         .update(cleanId)
                         .digest('hex');

console.log('\n======================================================');
console.log('          🔑 GENERADOR DE LICENCIAS REDROOM');
console.log('======================================================');
console.log(`📌 MACHINE ID CLIENTE : ${cleanId}`);
console.log(`🔒 SECRET KEY USADA    : ${secret === 'REDROOM_SECRET_KEY_2026' ? 'Por defecto (Desarrollo)' : 'Personalizada (.env)'}`);
console.log('------------------------------------------------------');
console.log(`👉 CLAVE DE LICENCIA  : ${licenseKey}`);
console.log('======================================================\n');
