const mongoose = require("mongoose");
const connectDB = require("../config/database");

async function up() {
  await connectDB();

  try {
    const collection = mongoose.connection.db.collection("headermenus");
    const result = await collection.updateMany(
      { is_maintainance: { $exists: false } },
      { $set: { is_maintainance: false } },
    );

    console.log(
      `Added is_maintainance=false to ${result.modifiedCount || 0} HeaderMenu document(s).`,
    );
  } catch (error) {
    console.error("Failed to add is_maintainance to HeaderMenu documents:", error);
    throw error;
  } finally {
    await mongoose.disconnect();
  }
}

async function down() {
  await connectDB();

  try {
    const collection = mongoose.connection.db.collection("headermenus");
    const result = await collection.updateMany(
      { is_maintainance: { $exists: true } },
      { $unset: { is_maintainance: "" } },
    );

    console.log(
      `Removed is_maintainance from ${result.modifiedCount || 0} HeaderMenu document(s).`,
    );
  } catch (error) {
    console.error("Failed to rollback is_maintainance on HeaderMenu documents:", error);
    throw error;
  } finally {
    await mongoose.disconnect();
  }
}

module.exports = { up, down };
