#!/usr/bin/env node

/**
 * Setup Script - Generate Bcrypt Hash for Admin Password
 * Usage: node setup.js
 */

const bcrypt = require('bcrypt');
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

console.log('\n' + '='.repeat(60));
console.log('🔐 Numerology CMS Backend - Setup Script');
console.log('='.repeat(60) + '\n');

rl.question('Enter admin password (default: admin123): ', (password) => {
  const adminPassword = password || 'admin123';

  console.log('\n⏳ Generating bcrypt hash...\n');

  bcrypt.hash(adminPassword, 10, (err, hash) => {
    if (err) {
      console.error('❌ Error generating hash:', err.message);
      rl.close();
      process.exit(1);
    }

    console.log('✅ Bcrypt Hash Generated:\n');
    console.log('Hash:', hash);
    console.log('\n' + '-'.repeat(60));
    console.log('📝 Instructions:');
    console.log('-'.repeat(60));
    console.log('\n1. Update the database.sql file:');
    console.log('   Replace the hash in this line:');
    console.log("   INSERT INTO admin (name, email, password, role) VALUES");
    console.log("   ('Super Admin', 'admin@numerology.com', '');");
    console.log('\n   With:');
    console.log(`   ('Super Admin', 'admin@numerology.com', '${hash}', 'superadmin');`);
    console.log('\n2. Or update directly in MySQL:');
    console.log(`   UPDATE admin SET password = '${hash}' WHERE email = 'admin@numerology.com';`);
    console.log('\n' + '-'.repeat(60) + '\n');

    rl.close();
  });
});

rl.on('close', () => {
  process.exit(0);
});
