const mongoose = require("mongoose");

const aboutUsSchema = new mongoose.Schema(
    {
        hero: {
            title: { type: String, trim: true, maxlength: 80 },
            breadcrumb: [{ type: String, trim: true, maxlength: 80 }],
            backgroundImage: { type: String, trim: true, maxlength: 255 },
        },
        intro: {
            subheading: { type: String, trim: true, maxlength: 80 },
            heading: { type: String, trim: true, maxlength: 120 },
            description: { type: String, trim: true, maxlength: 1000 },
            image: { type: String, trim: true, maxlength: 255 },
        },
        mission: {
            subheading: { type: String, trim: true, maxlength: 80 },
            heading: { type: String, trim: true, maxlength: 120 },
            description: { type: String, trim: true, maxlength: 1000 },
            images: {
                main: { type: String, trim: true, maxlength: 255 },
                secondary: { type: String, trim: true, maxlength: 255 },
                bgShape: { type: String, trim: true, maxlength: 255 },
                planeShape: { type: String, trim: true, maxlength: 255 },
                topShape: { type: String, trim: true, maxlength: 255 },
                globeShape: { type: String, trim: true, maxlength: 255 },
            },
            items: [
                new mongoose.Schema(
                    {
                        icon: { type: String, trim: true, maxlength: 255 },
                        label: { type: String, trim: true, maxlength: 80 },
                        description: { type: String, trim: true, maxlength: 240 },
                    },
                    { _id: false },
                ),
            ],
            features: [{ type: String, trim: true, maxlength: 80 }],
            ctaButton: {
                label: { type: String, trim: true, maxlength: 64 },
                href: { type: String, trim: true, maxlength: 255 },
            },
        },
        features: {
            backgroundImage: { type: String, trim: true, maxlength: 255 },
            subheading: { type: String, trim: true, maxlength: 80 },
            heading: { type: String, trim: true, maxlength: 120 },
            description: { type: String, trim: true, maxlength: 1000 },
            image: { type: String, trim: true, maxlength: 255 },
            items: [
                new mongoose.Schema(
                    {
                        icon: { type: String, trim: true, maxlength: 255 },
                        title: { type: String, trim: true, maxlength: 80 },
                        description: { type: String, trim: true, maxlength: 240 },
                    },
                    { _id: false },
                ),
            ],
            ctaButton: {
                label: { type: String, trim: true, maxlength: 64 },
                href: { type: String, trim: true, maxlength: 255 },
            },
        },
        news: {
            subheading: { type: String, trim: true, maxlength: 80 },
            heading: { type: String, trim: true, maxlength: 120 },
            ctaButton: {
                label: { type: String, trim: true, maxlength: 64 },
                href: { type: String, trim: true, maxlength: 255 },
            },
            selectedBlogIds: [{ type: mongoose.Schema.Types.ObjectId, ref: "Blog" }],
            // Deprecated: items field kept for backward compatibility during migration
            items: [
                new mongoose.Schema(
                    {
                        title: { type: String, trim: true, maxlength: 120 },
                        category: { type: String, trim: true, maxlength: 48 },
                        date: { type: String, trim: true, maxlength: 32 },
                        comments: Number,
                        author: {
                            name: { type: String, trim: true, maxlength: 48 },
                            avatar: { type: String, trim: true, maxlength: 255 },
                        },
                        link: { type: String, trim: true, maxlength: 255 },
                        thumbnail: { type: String, trim: true, maxlength: 255 },
                    },
                    { _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);
