// Simple test to query the database directly
const Database = require('better-sqlite3');
const path = require('path');

console.log('[OK] Testing database connection...');

try {
    // Use the same path as the server
    const dbPath = 'C:\\Shares\\testdatadb\\database\\testdata.db';

    console.log(`[OK] Opening database: ${dbPath}`);
    const db = new Database(dbPath, { readonly: true });

    console.log('[OK] Database opened successfully');

    // Test simple query
    console.log('[OK] Running test query: SELECT COUNT(*) FROM test_records');
    const result = db.prepare('SELECT COUNT(*) as count FROM test_records').get();

    console.log(`[OK] Total records: ${result.count}`);

    // Test another query
    console.log('[OK] Running test query: SELECT * FROM test_records LIMIT 1');
    const record = db.prepare('SELECT * FROM test_records LIMIT 1').get();

    if (record) {
        console.log('[OK] Sample record retrieved:');
        console.log(`  ID: ${record.id}`);
        console.log(`  Model: ${record.model_number}`);
        console.log(`  Serial: ${record.serial_number}`);
        console.log(`  Date: ${record.test_date}`);
    }

    db.close();
    console.log('[OK] Database closed successfully');
    console.log('[SUCCESS] Database is working correctly!');

} catch (err) {
    console.error('[ERROR] Database test failed:');
    console.error(err.message);
    console.error(err.stack);
    process.exit(1);
}
