import mongoose from "mongoose";
import dotenv from "dotenv";

// Load environment variables
dotenv.config();

const memberSchema = new mongoose.Schema({
  name: String,
  avatar: String,
  email: String,
});

const Member = mongoose.model("Member", memberSchema);

const validAvatars = [
  "disbelieving",
  "double-take",
  "exasperated",
  "mild",
  "sweet",
];

async function fixAvatars() {
  try {
    await mongoose.connect(
      process.env.NUXT_MONGODB_URI || process.env.MONGODB_URI,
    );
    console.log("Connected to MongoDB");

    const members = await Member.find({ avatar: { $exists: true, $ne: "" } });
    console.log(`\nFound ${members.length} members with avatars set`);

    for (const member of members) {
      if (!validAvatars.includes(member.avatar?.toLowerCase())) {
        console.log(`\n❌ Invalid avatar found:`);
        console.log(`   Email: ${member.email}`);
        console.log(`   Name: ${member.name}`);
        console.log(`   Current avatar: "${member.avatar}"`);

        // Set to a default valid avatar
        member.avatar = "mild";
        await member.save();
        console.log(`   ✅ Fixed to: "mild"`);
      } else {
        console.log(`✓ ${member.email} - avatar: "${member.avatar}" (valid)`);
      }
    }

    await mongoose.disconnect();
    console.log("\n✅ Done!");
    process.exit(0);
  } catch (error) {
    console.error("Error:", error);
    process.exit(1);
  }
}

fixAvatars();
