import mongoose from "mongoose";

const MONGODB_URI = "mongodb+srv://aliraza68629:X8qR2Dkp5jlXwPZk@cluster0.xsufbzs.mongodb.net/CSSAwwwards?retryWrites=true&w=majority&appName=Cluster0";

const dropIndexes = async () => {
  try {
    console.log("🔌 Connecting to MongoDB...");
    
    await mongoose.connect(MONGODB_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });

    console.log("✅ Connected to MongoDB");

    const db = mongoose.connection.db;
    const collection = db.collection('settingprofiles');

    // Get all indexes
    console.log("\n📋 Current indexes:");
    const indexes = await collection.indexes();
    indexes.forEach(index => {
      console.log(`  - ${index.name}:`, index.key);
    });

    // Drop username index if it exists
    try {
      await collection.dropIndex('username_1');
      console.log("\n✅ Dropped username_1 index");
    } catch (err) {
      console.log("\n⚠️  username_1 index doesn't exist or already dropped");
    }

    // Drop url index if it exists
    try {
      await collection.dropIndex('url_1');
      console.log("✅ Dropped url_1 index");
    } catch (err) {
      console.log("⚠️  url_1 index doesn't exist or already dropped");
    }

    // Show remaining indexes
    console.log("\n📋 Remaining indexes:");
    const remainingIndexes = await collection.indexes();
    remainingIndexes.forEach(index => {
      console.log(`  - ${index.name}:`, index.key);
    });

    console.log("\n✅ Index cleanup completed!");
    process.exit(0);

  } catch (error) {
    console.error("❌ Error:", error.message);
    process.exit(1);
  }
};

dropIndexes();
