require("dotenv").config();
const fs = require("fs").promises;
const path = require("path");
const connectDB = require("../config/database");
const Contact = require("../models/contact");
const mongoose = require("mongoose");

/**
 * Migration: contact
 * Migrate contact data from contact-data.json
 */
async function migrate() {
  try {
    await connectDB();

    // Read contact-data.json file
    const contactJsonPath = path.join(__dirname, "../data/contact.json");
    const contactData = JSON.parse(await fs.readFile(contactJsonPath, "utf8"));

    // Migrate data using the model's static method
    await Contact.migrateFromJson(contactData);

    console.log("Contact migration completed successfully");

    await mongoose.disconnect();
    process.exit(0);
  } catch (error) {
    console.error("Migration error:", error);
    process.exit(1);
  }
}

// Chạy migration nếu được gọi trực tiếp
if (require.main === module) {
  migrate();
}

module.exports = { migrate };
