const mysql = require('mysql2/promise');
const net = require('net');

const config = {
    host: "175.103.40.219",
    port: 3307,
    user: "root",
    password: "S@pAbadi**ICT",
    database: "sapharmaco_sapps_uat",
    connectTimeout: 5000
};

console.log("Testing TCP connection to " + config.host + ":" + config.port + "...");

const socket = new net.Socket();
socket.setTimeout(5000);

socket.on('connect', () => {
    console.log('TCP Connection SUCCESS!');
    socket.destroy();
    testMysql();
});

socket.on('timeout', () => {
    console.log('TCP Connection TIMEOUT');
    socket.destroy();
});

socket.on('error', (err) => {
    console.log('TCP Connection ERROR:', err.code, err.message);
});

socket.connect(config.port, config.host);

async function testMysql() {
    console.log("\nTesting MySQL Connection...");
    try {
        const connection = await mysql.createConnection(config);
        console.log("MySQL Connection SUCCESS!");
        await connection.end();
    } catch (err) {
        console.log("MySQL Connection FAILED:", err.code, err.message);
    }
}
