const mongoose = require("mongoose");
const Footer = require("../models/footer");
const footerData = require("../data/footer.json");

async function addFooterMenuOrder() {
    try {
        console.log("=== Adding order field to Footer Menu Links ===");

        // Connect to database
        await mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/hailearning");
        console.log("✓ Connected to MongoDB");

        // Get existing footer or create from JSON
        let footer = await Footer.findOne();

        if (!footer) {
            console.log("No existing footer found, creating from JSON data...");

            // Add order to bottom menu links
            if (footerData.bottom && footerData.bottom.menuLinks) {
                footerData.bottom.menuLinks = footerData.bottom.menuLinks.map((link, index) => ({
                    ...link,
                    order: index + 1,
                }));
            }

            // Add order to top menu links
            if (footerData.top && footerData.top.menuLinks) {
                footerData.top.menuLinks = footerData.top.menuLinks.map((link, index) => ({
                    ...link,
                    order: index + 1,
                }));
            }

            footer = await Footer.create(footerData);
            console.log("✓ Footer created with order fields");
        } else {
            console.log("Found existing footer, adding order fields...");

            // Add order to bottom menu links
            if (footer.bottom && footer.bottom.menuLinks) {
                footer.bottom.menuLinks = footer.bottom.menuLinks.map((link, index) => ({
                    label: link.label,
                    href: link.href,
                    order: link.order || index + 1,
                }));
            }

            // Add order to top menu links
            if (footer.top && footer.top.menuLinks) {
                footer.top.menuLinks = footer.top.menuLinks.map((link, index) => ({
                    label: link.label,
                    href: link.href,
                    order: link.order || index + 1,
                }));
            }

            await footer.save();
            console.log("✓ Footer updated with order fields");
        }

        console.log("Bottom Menu Links with order:");
        footer.bottom.menuLinks.forEach((link, index) => {
            console.log(`  ${index + 1}. ${link.label} (order: ${link.order}) -> ${link.href}`);
        });

        console.log("=== Footer Menu Order Migration Completed ===");
    } catch (error) {
        console.error("✗ Migration failed:", error);
        throw error;
    } finally {
        await mongoose.disconnect();
        console.log("✓ Disconnected from MongoDB");
    }
}

// Run migration if called directly
if (require.main === module) {
    addFooterMenuOrder()
        .then(() => {
            console.log("Migration completed successfully");
            process.exit(0);
        })
        .catch((error) => {
            console.error("Migration failed:", error);
            process.exit(1);
        });
}

module.exports = addFooterMenuOrder;
