var Schema = require('mongoose').Schema;

let videoStorySchema = new Schema({
  name: { type: String, required: true, searchable: true, desc: '视频故事名' },
  url: { type: String, required: true, unique: true, desc: '视频url' },
  enabled: { type: Boolean, default: false, index: true, desc: '启用' },
  poster: { type: Schema.Types.ObjectId, ref: 'ArtBin', require: true, desc: '海报图' },
  title: { type: Schema.Types.ObjectId, ref: 'Translate', desc: '名称' }, //支持多语言， populate from coloring_ol db
  slogon: { type: Schema.Types.ObjectId, ref: 'Translate', desc: '口号' }, //支持多语言， populate from coloring_ol db
  seoTitle: { type: String, desc: '专辑标题', searchable: true }, // 新增字段，网站title，60个字符以内， json字符串，形如： {zh: '中国', en: 'China'}
  seoDescription: { type: String, desc: '专辑描述', searchable: true },  // 新增字段，网站seo meta  description， 160个字符以内， json字符串，形如： {zh: '中国', en: 'China'}
  order: { type: Number, default: 1000, index: true, desc: '排序', orderable: true, },
  contents: { type: [Schema.Types.ObjectId], ref: 'Art', default: [], desc: '内容' }, // 只存内容id，结合popuate
  user: { type: Schema.Types.ObjectId, ref: 'User', required: true, index: true, desc: '创建者' },
  timeCreate: { type: Date, index: true, desc: '创建时间', default: Date.now, orderable: true, },
  timeLastModify: { type: Date, index: true, desc: '最后修改时间', default: Date.now, orderable: true, },
}, {
  strict: true,
  toJSON: {
    virtuals: true,
    transform: storyTransform,
  },
  toObject: {
    virtuals: true,
    transform: storyTransform,
  },

});

function storyTransform(doc, ret) {
  ret.poster = `/res/coloring/vs-poster/480/${doc._id}.png`;
  ret.size = doc.contents ? doc.contents.length : 0;
}

module.exports = videoStorySchema;
