const mongoose = require("mongoose");

const aboutUsSchema = new mongoose.Schema(
    {
        hero: {
            title: String,
            breadcrumb: [String],
            backgroundImage: String,
        },
        intro: {
            subheading: String,
            heading: String,
            description: String,
            image: String,
        },
        mission: {
            subheading: String,
            heading: String,
            description: String,
            images: {
                main: String,
                secondary: String,
                bgShape: String,
                planeShape: String,
                topShape: String,
                globeShape: String,
            },
            items: [
                new mongoose.Schema(
                    {
                        icon: String,
                        label: String,
                        description: String,
                    },
                    { _id: false },
                ),
            ],
            features: [String],
            ctaButton: {
                label: String,
                href: String,
            },
        },
        features: {
            backgroundImage: String,
            subheading: String,
            heading: String,
            description: String,
            image: String,
            items: [
                new mongoose.Schema(
                    {
                        icon: String,
                        title: String,
                        description: String,
                    },
                    { _id: false },
                ),
            ],
            ctaButton: {
                label: String,
                href: String,
            },
        },
        news: {
            subheading: String,
            heading: String,
            ctaButton: {
                label: String,
                href: String,
            },
            selectedBlogIds: [{ type: mongoose.Schema.Types.ObjectId, ref: "Blog" }],
            // Deprecated: items field kept for backward compatibility during migration
            items: [
                new mongoose.Schema(
                    {
                        title: String,
                        category: String,
                        date: String,
                        comments: Number,
                        author: {
                            name: String,
                            avatar: String,
                        },
                        link: String,
                        thumbnail: String,
                    },
                    { _id: false },
                ),
            ],
        },
    },
    {
        timestamps: true,
        collection: "aboutus",
    },
);

// Static method để đảm bảo luôn chỉ có 1 bản ghi duy nhất (Singleton)
aboutUsSchema.statics.getSingle = async function () {
    let doc = await this.findOne();
    if (!doc) {
        doc = await this.create({});
    }
    return doc;
};

module.exports = mongoose.model("AboutUs", aboutUsSchema);
