const mongoose = require('mongoose');
const uri = "mongodb+srv://admtracksteel:29OHAHpKTI8XcCNt@cluster0.a4xiilu.mongodb.net/ts_gpi?retryWrites=true&w=majority&appName=Cluster0";

async function checkClerk() {
    try {
        console.log('Connecting to MongoDB...');
        await mongoose.connect(uri);
        console.log('Connected.');

        const db = mongoose.connection.db;
        const collections = await db.listCollections().toArray();
        
        let foundClerkGlobal = false;

        for (const collInfo of collections) {
            const collection = db.collection(collInfo.name);
            
            // Search for documents having clerkId or clerkUserId
            const anyClerk = await collection.findOne({
                $or: [
                    { clerkId: { $exists: true } },
                    { clerkUserId: { $exists: true } },
                    { userId: /clerk/i },
                    { email: /clerk/i }
                ]
            });

            if (anyClerk) {
                console.log(`[!] Found Clerk-related data in collection: ${collInfo.name}`);
                console.log('Example document keys:', Object.keys(anyClerk));
                foundClerkGlobal = true;
            } else {
                console.log(`[ ] No obvious Clerk data in: ${collInfo.name}`);
            }
        }

        if (!foundClerkGlobal) {
            console.log('>>> NO CLERK DATA FOUND IN ANY COLLECTION <<<');
        }

        await mongoose.disconnect();
        console.log('Done.');
    } catch (err) {
        console.error('Error:', err);
        process.exit(1);
    }
}

checkClerk();
