import mongoose from 'mongoose';
import connectToDatabase from '../config/dbConfig';

(async () => {
  try {
    // 1️⃣ Connect to MongoDB
    await connectToDatabase();
    console.log('✅ Connected to MongoDB');

    // 2️⃣ Get the collection
    const collection = mongoose.connection.collection('whatsapps');

    // 3️⃣ Drop the index
    const indexName = 'templateId_1';
    const result = await collection.dropIndex(indexName);

    console.log(`🗑️  Dropped index: ${indexName}`);
    console.log('Result:', result);
    process.exit(0);
  } catch (error) {
    if (error.codeName === 'IndexNotFound')
      console.log('⚠️  Index not found or already removed.');
    else console.error('❌ Error dropping index:', error);
    process.exit(0);
  } finally {
    // 5️⃣ Exit process cleanly
    process.exit(0);
  }
})();
