import mongoose from "mongoose";

const ComponentSchema = new mongoose.Schema(
  {
    componentType: { type: String, required: true, trim: true }, // e.g., "HeroSection", "AI_Automation", "WhyProjectFail"
    componentName: { type: String, required: true, trim: true }, // Display name
    data: { type: mongoose.Schema.Types.Mixed, required: true }, // Flexible JSON data
    order: { type: Number, default: 0 },
    enabled: { type: Boolean, default: true },
  },
  { _id: true, timestamps: true }
);

const BlogSchema = new mongoose.Schema(
  {
    category: {
      type: String,
      required: true,
      trim: true,
      unique: true,
      enum: [
        "Blogs-AI-Automation",
        "Blogs-Business-Growth",
        "Blogs-Custom-Software",
        "Blogs-Custom-Marketing",
        "Blogs-IT-Staff",
        "Blogs-Mobile-App",
        "Blogs-Social-Marketing",
        "Blogs-Web-Design",
      ],
    },
    title: { type: String, required: true, trim: true, maxlength: 200 },
    description: { type: String, trim: true },
    heroSection: {
      category: { type: String, trim: true },
      title: { type: String, trim: true },
      description: { type: String, trim: true },
    },
    components: { type: [ComponentSchema], default: [] },
    status: { type: String, enum: ["Draft", "Published"], default: "Draft" },
  },
  { timestamps: true }
);

BlogSchema.index({ category: 1 });
BlogSchema.index({ createdAt: -1 });

const BlogModel = mongoose.models.Blog || mongoose.model("Blog", BlogSchema);

export { BlogModel };

