/**
 * Update IP Mesin di Database fin_pro.device ke 192.168.0.50
 */
const { execSync } = require('child_process');
const fs = require('fs');

const MYSQL_EXES = [
    'C:\\laragon\\bin\\mysql\\mysql-8.4.3-winx64\\bin\\mysql.exe',
    'C:\\laragon\\bin\\mysql\\mysql-8.0.30-winx64\\bin\\mysql.exe',
    'C:\\laragon\\bin\\mysql\\mysql-5.7.33-winx64\\bin\\mysql.exe',
    'C:\\Program Files (x86)\\Fingerspot Personnel\\mysql.exe',
];

let mysqlCmd = 'mysql';
for (const p of MYSQL_EXES) {
    if (fs.existsSync(p)) {
        mysqlCmd = `"${p}"`;
        break;
    }
}

const PASS = 'Coba lagi lain kali 2015';

function runSql(query) {
    const cmd = `${mysqlCmd} -h 127.0.0.1 -P 3309 -u root -p"${PASS}" -D fin_pro -e "${query}"`;
    return execSync(cmd, { encoding: 'utf8', timeout: 5000 });
}

console.log('=======================================================');
console.log(' UPDATING DEVICE IP IN fin_pro.device');
console.log('=======================================================');

try {
    console.log('1. IP Sebelum Update:');
    console.log(runSql("SELECT sn, device_name, ip_address, ethernet_port FROM device;"));

    console.log('\n2. Mengubah IP ke 192.168.0.50 & Port ke 8080...');
    runSql("UPDATE device SET ip_address='192.168.0.50', ethernet_port='8080' WHERE sn='616231023290570';");

    console.log('\n3. IP Setelah Update:');
    console.log(runSql("SELECT sn, device_name, ip_address, ethernet_port FROM device;"));

    console.log('\n✅ UPDATE BERHASIL! Database Fingerspot Personnel sekarang mengarah ke 192.168.0.50:8080');

} catch (err) {
    console.log(`❌ Error: ${err.message}`);
}

console.log('=======================================================\n');
