const mongodb = require('mongodb');
require('dotenv').config({ path: require('path').join(__dirname, '.env') });

const uri = process.env.CONNECTION_URI;
console.log('Testing with URI:', uri);

const uriUrl = new URL(uri);
console.log('URL components:');
console.log('  hostname:', uriUrl.hostname);
console.log('  port:', uriUrl.port);
console.log('  pathname:', uriUrl.pathname);
console.log('  searchParams:', Object.fromEntries(uriUrl.searchParams));

// Try to connect directly
mongodb.MongoClient.connect(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    replicaSet: 'rs0',
    authSource: 'admin'
}, (err, client) => {
    if (err) {
        console.error('Connection error:', err.message);
    } else {
        console.log('✅ Connected successfully!');
        client.close();
    }
});
