const mongoose = require("mongoose"); // ── Sub-schemas ────────────────────────────────────────────────────────────── const metadataSchema = new mongoose.Schema( { title: { type: String, default: "" }, description: { type: String, default: "" }, keywords: { type: String, default: "" }, ogImage: { type: String, default: "" }, }, { _id: false }, ); const valuePropSchema = new mongoose.Schema( { icon: { type: String, default: "" }, title: { type: String, default: "" }, description: { type: String, default: "" }, }, { _id: false }, ); const heroSchema = new mongoose.Schema( { badge: { type: String, default: "" }, titleMain: { type: String, default: "" }, titleHighlight: { type: String, default: "" }, description: { type: String, default: "" }, valueProps: { type: [valuePropSchema], default: [] }, }, { _id: false }, ); const formOptionSchema = new mongoose.Schema( { label: { type: String, default: "" }, value: { type: String, default: "" }, }, { _id: false }, ); const formFieldSchema = new mongoose.Schema( { name: { type: String, default: "" }, label: { type: String, default: "" }, placeholder: { type: String, default: "" }, required: { type: Boolean, default: false }, }, { _id: false }, ); const formSelectFieldSchema = new mongoose.Schema( { name: { type: String, default: "" }, label: { type: String, default: "" }, placeholder: { type: String, default: "" }, required: { type: Boolean, default: false }, options: { type: [formOptionSchema], default: [] }, }, { _id: false }, ); const formTimelineFieldSchema = new mongoose.Schema( { name: { type: String, default: "" }, label: { type: String, default: "" }, options: { type: [formOptionSchema], default: [] }, }, { _id: false }, ); const formFieldsSchema = new mongoose.Schema( { firstName: { type: formFieldSchema, default: () => ({}) }, lastName: { type: formFieldSchema, default: () => ({}) }, email: { type: formFieldSchema, default: () => ({}) }, phone: { type: formFieldSchema, default: () => ({}) }, program: { type: formSelectFieldSchema, default: () => ({}) }, timeline: { type: formTimelineFieldSchema, default: () => ({}) }, }, { _id: false }, ); const formSchema = new mongoose.Schema( { heading: { type: String, default: "" }, description: { type: String, default: "" }, fields: { type: formFieldsSchema, default: () => ({}) }, consentText: { type: String, default: "" }, submitLabel: { type: String, default: "" }, footerText: { type: String, default: "" }, successMessage: { type: String, default: "" }, errorMessage: { type: String, default: "" }, }, { _id: false }, ); // ── Root schema ─────────────────────────────────────────────────────────────── const requestInfoSchema = new mongoose.Schema( { slug: { type: String, required: true, unique: true, default: "request-info", }, metadata: { type: metadataSchema, default: () => ({}) }, hero: { type: heroSchema, default: () => ({}) }, form: { type: formSchema, default: () => ({}) }, }, { timestamps: true }, ); /** * Seed / migrate từ file JSON (cùng shape với API frontend). * Idempotent — chạy lại không tạo trùng. */ requestInfoSchema.statics.migrateFromJson = async function (jsonData) { const slug = "request-info"; const payload = { slug, metadata: jsonData.metadata || {}, hero: jsonData.hero || {}, form: jsonData.form || {}, }; const existing = await this.findOne({ slug }); if (existing) { existing.set(payload); await existing.save(); return existing; } return this.create(payload); }; module.exports = mongoose.model("RequestInfo", requestInfoSchema);