/**
 * Migration script for Appointment data
 * Imports data from appointment.json to MongoDB
 *
 * Run: node scripts/2026_02_03_appointment.js
 */

require("dotenv").config();
const mongoose = require("mongoose");
const fs = require("fs");
const path = require("path");

// Connect to MongoDB
const connectDB = async () => {
    try {
        await mongoose.connect(process.env.MONGODB_URI);
        console.log("MongoDB connected successfully");
    } catch (error) {
        console.error("MongoDB connection error:", error);
        process.exit(1);
    }
};

const runMigration = async () => {
    try {
        await connectDB();

        // Load Appointment model
        const Appointment = require("../models/appointment");

        // Load JSON data
        const jsonPath = path.join(__dirname, "../data/appointment.json");

        if (!fs.existsSync(jsonPath)) {
            console.log("appointment.json not found, creating default data...");
            const defaultData = {
                hero: {
                    title: "Make Appointment",
                    backgroundImage: "",
                    subtitle: "",
                    heading: "",
                    description: "",
                },
                visaOptions: [],
                form: {
                    heading: "Request Appointment",
                    fields: [],
                    submitButton: {
                        text: "Request Appointment",
                        icon: "fa-solid fa-arrow-right",
                        buttonClass: "theme-btn",
                    },
                },
            };
            await Appointment.migrateFromJson(defaultData);
        } else {
            const jsonData = JSON.parse(fs.readFileSync(jsonPath, "utf8"));
            console.log("Loaded appointment.json data");
            await Appointment.migrateFromJson(jsonData);
        }

        console.log("✅ Appointment migration completed successfully!");
    } catch (error) {
        console.error("❌ Migration failed:", error);
    } finally {
        await mongoose.connection.close();
        console.log("MongoDB connection closed");
    }
};

runMigration();
