import mongoose, { Schema, models, model } from "mongoose";

export interface BlogPostDoc {
  _id: string;
  title: string;
  titleEn: string;
  slug: string;
  excerpt: string;
  excerptEn: string;
  // Fließtext mit Absätzen (Zeilenumbrüchen) — bewusst kein Rich-Text-Editor,
  // "kurze Beiträge" laut Planung, keine vollwertige CMS-Funktionalität nötig.
  content: string;
  contentEn: string;
  coverImage: string;
  tags: string[];
  publishedAt: Date;
  visible: boolean;
  createdAt: Date;
  updatedAt: Date;
}

const blogPostSchema = new Schema<BlogPostDoc>(
  {
    title: { type: String, required: true, trim: true, maxlength: 150 },
    titleEn: { type: String, default: "", trim: true, maxlength: 150 },
    slug: { type: String, required: true, unique: true, trim: true, lowercase: true, maxlength: 180 },
    excerpt: { type: String, default: "", trim: true, maxlength: 300 },
    excerptEn: { type: String, default: "", trim: true, maxlength: 300 },
    content: { type: String, required: true, trim: true, maxlength: 8000 },
    contentEn: { type: String, default: "", trim: true, maxlength: 8000 },
    coverImage: { type: String, default: "" },
    tags: { type: [String], default: [] },
    publishedAt: { type: Date, default: Date.now, index: true },
    visible: { type: Boolean, default: true },
  },
  { timestamps: true }
);

export const BlogPost =
  (models.BlogPost as mongoose.Model<BlogPostDoc>) || model<BlogPostDoc>("BlogPost", blogPostSchema);
