import 'dotenv/config';
import mongoose from 'mongoose';
import WebsiteSubmission from './models/websiteSubmission.js';

async function dropDuplicateIndexes() {
  try {
    // Connect to MongoDB
    await mongoose.connect(process.env.MONGO_URI);

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

    // Get existing indexes
    const indexes = await WebsiteSubmission.collection.getIndexes();
    console.log('\n📋 Current indexes:');
    console.log(JSON.stringify(indexes, null, 2));

    // Drop all indexes except _id
    console.log('\n🗑️ Dropping all indexes except _id...');
    await WebsiteSubmission.collection.dropIndexes();
    console.log('✅ Indexes dropped');

    // Recreate indexes from schema
    console.log('\n🔨 Creating indexes from schema...');
    await WebsiteSubmission.syncIndexes();
    console.log('✅ Indexes recreated');

    // Show new indexes
    const newIndexes = await WebsiteSubmission.collection.getIndexes();
    console.log('\n📋 New indexes:');
    console.log(JSON.stringify(newIndexes, null, 2));

    console.log('\n✅ Successfully fixed duplicate indexes!');
    process.exit(0);
  } catch (error) {
    console.error('❌ Error:', error);
    process.exit(1);
  }
}

dropDuplicateIndexes();
