import mongoose from "mongoose";
import SettingProfile from "./models/settingProfile.js";

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

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

  try {
    const profiles = await SettingProfile.find({}).lean();
    
    console.log(`\n📊 Total profiles: ${profiles.length}\n`);
    
    profiles.forEach((profile, index) => {
      console.log(`Profile ${index + 1}:`);
      console.log(`  ID: ${profile._id}`);
      console.log(`  Username: ${profile.username}`);
      console.log(`  Display Name: ${profile.displayName}`);
      console.log(`  URL: ${profile.url}`);
      console.log(`  Submitted By: ${profile.submittedBy}`);
      console.log(`  Created: ${profile.createdAt}`);
      console.log(`---`);
    });

    // Check for duplicates
    const usernameMap = {};
    const urlMap = {};
    
    profiles.forEach(profile => {
      if (usernameMap[profile.username]) {
        console.log(`\n⚠️  DUPLICATE USERNAME: "${profile.username}"`);
        console.log(`   Profile 1 ID: ${usernameMap[profile.username]}`);
        console.log(`   Profile 2 ID: ${profile._id}`);
      } else {
        usernameMap[profile.username] = profile._id;
      }

      if (profile.url && urlMap[profile.url]) {
        console.log(`\n⚠️  DUPLICATE URL: "${profile.url}"`);
        console.log(`   Profile 1 ID: ${urlMap[profile.url]}`);
        console.log(`   Profile 2 ID: ${profile._id}`);
      } else {
        urlMap[profile.url] = profile._id;
      }
    });

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

checkProfiles();
