const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config({ path: path.join(__dirname, "../.env") });

const Header = require("../models/header");

async function updateHeaderData() {
    try {
        // Connect to MongoDB
        await mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/hailearning");
        console.log("Connected to MongoDB");

        // Find the first header
        let header = await Header.findOne().sort({ order: 1 });

        if (!header) {
            console.log("No header found, creating new one...");
            header = new Header({
                top: {
                    phone: "+09 378 357 5222",
                    email: "info@hailearning.edu.vn",
                    location: "69 Street, 5th Avenue LA, United States",
                    socialLinks: [
                        {
                            platform: "linkedin",
                            url: "https://linkedin.com",
                            icon: "fa-brands fa-linkedin",
                        },
                        {
                            platform: "twitter",
                            url: "https://twitter.com",
                            icon: "fa-brands fa-twitter",
                        },
                        {
                            platform: "instagram",
                            url: "https://instagram.com",
                            icon: "fa-brands fa-instagram",
                        },
                        {
                            platform: "youtube",
                            url: "https://youtube.com",
                            icon: "fa-brands fa-youtube",
                        },
                    ],
                    languages: [
                        { name: "English", value: "1" },
                        { name: "Bangla", value: "2" },
                        { name: "Hindi", value: "3" },
                    ],
                },
                status: "active",
                order: 1,
            });
        } else {
            console.log("Header found, updating...");
            // Update existing header
            header.top = {
                phone: header.top?.phone || "+09 378 357 5222",
                email: header.top?.email || "info@hailearning.edu.vn",
                location: header.top?.location || "69 Street, 5th Avenue LA, United States",
                socialLinks:
                    header.top?.socialLinks?.length > 0
                        ? header.top.socialLinks
                        : [
                              {
                                  platform: "linkedin",
                                  url: "https://linkedin.com",
                                  icon: "fa-brands fa-linkedin",
                              },
                              {
                                  platform: "twitter",
                                  url: "https://twitter.com",
                                  icon: "fa-brands fa-twitter",
                              },
                              {
                                  platform: "instagram",
                                  url: "https://instagram.com",
                                  icon: "fa-brands fa-instagram",
                              },
                              {
                                  platform: "youtube",
                                  url: "https://youtube.com",
                                  icon: "fa-brands fa-youtube",
                              },
                          ],
                languages: header.top?.languages || [
                    { name: "English", value: "1" },
                    { name: "Bangla", value: "2" },
                    { name: "Hindi", value: "3" },
                ],
            };
        }

        await header.save();
        console.log("Header updated successfully!");
        console.log("Header data:", JSON.stringify(header, null, 2));

        await mongoose.connection.close();
        console.log("Database connection closed");
    } catch (error) {
        console.error("Error updating header:", error);
        process.exit(1);
    }
}

updateHeaderData();
