const mongoose = require('mongoose')
const TABLE_NAME = 'Post'
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId
const escape = (require('../utils')).escape

const PostSchema = new Schema({
  //类型
  type: {
    type: String,
    default: 'post' // post | page
  },
  //标题
  title: {
    type: String,
    trim: true,
    set: escape
  },
  //别名
  alias: {
    type: String,
    trim: true,
    set: escape
  },
  //创建者
  user: {
    type: ObjectId,
    ref: 'User'
  },
  //类别
  category: {
    type: ObjectId,
    ref: 'Category'
  },
  //摘要
  excerpt: {
    type: String,
    trim: true,
  },
  //内容
  contents: {
    type: String,
    trim: true,
  },
  //markdown
  markdown: {
    type: String,
    trim: true,
  },
  //标签
  tags: Array,
  //缩略图
  thumbnail: {
    type: String,
    trim: true,
    set: escape
  },
  //统计
  count: {
    //浏览次数
    views: {
      type: Number,
      default: 1,
    },
    //评论数
    comments: {
      type: Number,
      default: 0,
    },
    //点赞数
    praises: {
      type: Number,
      default: 1
    }
  },
  //状态
  status: {
    type: Number,
    default: 1 // 1：发布， 0 ：草稿， -1 ：删除
  },
  //置顶
  top: Boolean,
  //允许评论
  allowComment: {
    type: Boolean,
    default: true
  },
  //允许打赏
  allowReward: Boolean,
  //著名版权
  license: Boolean,
  //使用密码
  usePassword: Boolean,
  //密码
  password: {
    type: String,
    trim: true
  },
  order: {
    type: Number,
    default: 1
  },
  //创建时间
  createTime: {
    type: Date,
    default: Date.now()
  },
  //修改时间
  updateTime: {
    type: Date,
    default: Date.now()
  }
}, {
  connection: TABLE_NAME,
  versionKey: false,
})

module.exports = mongoose.model(TABLE_NAME, PostSchema)