import mongoose from "mongoose";

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(
      "mongodb+srv://aliraza68629:X8qR2Dkp5jlXwPZk@cluster0.hrnnnly.mongodb.net/CSSAwwwards?retryWrites=true&w=majority"
    );
    console.log(`✅ MongoDB Connected: ${conn.connection.host}`);
    return conn;
  } catch (error) {
    console.error(`❌ Error: ${error.message}`);
    process.exit(1);
  }
};

const fixIndexes = async () => {
  await connectDB();

  try {
    const collection = mongoose.connection.collection('settingprofiles');
    
    console.log('\n📋 Current indexes:');
    const indexes = await collection.indexes();
    indexes.forEach(index => {
      console.log(`  - ${index.name}:`, JSON.stringify(index.key));
    });

    console.log('\n🗑️  Dropping old indexes...');
    
    try {
      await collection.dropIndex('username_1');
      console.log('✅ Dropped username_1 index');
    } catch (err) {
      console.log('⚠️  username_1 index not found or already dropped');
    }

    console.log('\n✅ Database indexes fixed!');
    console.log('🔄 Please restart your backend server for changes to take effect.');
    
    process.exit(0);
  } catch (error) {
    console.error("❌ Error:", error);
    process.exit(1);
  }
};

fixIndexes();
