var Schema = require('mongoose').Schema;
const config = require('../config/app');

let albumSchema = new Schema({
  pid: { type: String, require: true, index: true, desc: '产品id' },
  tag: { type: String, required: true, desc: 'TAG' }, // 专辑唯一标识
  icon: { type: Schema.Types.ObjectId, ref: 'ArtBin', require: true, desc: 'icon' },
  cover: { type: Schema.Types.ObjectId, ref: 'ArtBin', require: true, desc: '头图' },
  enabled: { type: Boolean, default: false, index: 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
  order: { type: Number, default: 1000, index: true, desc: '排序', orderable: true, },
  contents: { type: [Schema.Types.ObjectId], ref: 'Art', default: [], desc: '内容' }, // 只存内容id，结合popuate
  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: albumTransform,
  },
  toObject: {
    virtuals: true,
    transform: albumTransform,
  },

});

function albumTransform(doc, ret) {
  ret.icon = `${config.resHost}/res/coloring/album_icon/640/${doc._id}.png`;
  ret.cover = `${config.resHost}/res/coloring/album_cover/720/${doc._id}.png`;
  ret.size = doc.contents ? doc.contents.length : 0;
}


module.exports = albumSchema;