require('dotenv').config();
const mysql = require('mysql2/promise');

async function checkBilling() {
    console.log('Testing connection to BILLING Database...');
    console.log(`Host: ${process.env.BILLING_DB_HOST}`);
    console.log(`User: ${process.env.BILLING_DB_USER}`);
    console.log(`Web DB: ${process.env.BILLING_DB_NAME}`);

    try {
        const connection = await mysql.createConnection({
            host: process.env.BILLING_DB_HOST,
            user: process.env.BILLING_DB_USER,
            password: process.env.BILLING_DB_PASS,
            database: process.env.BILLING_DB_NAME,
            connectTimeout: 10000 // 10s timeout
        });

        console.log('Connection successful!');

        // Test Select
        const [rows] = await connection.execute('SELECT COUNT(*) as count FROM customer');
        console.log('Test Select successful. Customer Count:', rows[0].count);

        await connection.end();
        console.log('BILLING DB TEST PASSED');
    } catch (e) {
        console.error('BILLING DB Test Failed:', e.message);
        if (e.code === 'ETIMEDOUT') {
            console.error('Hint: Check firewall or IP whitelist on the remote server.');
        }
    }
}

checkBilling();
