/**
 * Cek 6 Mesin Fingerprint di Database Fingerspot & Laravel
 */
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(' INSPEKSI MESIN FINGERSPOT (FIN_PRO.DEVICE & ATT_LOG)');
console.log('=======================================================');

try {
    console.log('\n--- Daftar Seluruh Mesin di Database Fingerspot (device) ---');
    console.log(runSql('SELECT sn, device_name, ip_address, ethernet_port, comm_type FROM device;'));

    console.log('\n--- Jumlah Distinct Mesin (SN) di Record Absensi (att_log) ---');
    console.log(runSql('SELECT sn, count(*) as total_scans, MAX(scan_date) as scan_terakhir FROM att_log GROUP BY sn;'));

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

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