
/**
 * Module dependencies.
 */

var mongoose = require('mongoose');
var Imager = rekuire('imager');
var config = rekuire('config');
var imagerConfig = rekuire(config.root + '/config/imager.js');
var utils = require('../../lib/utils')

var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

/**
 * Getters
 */

var getTags = function (tags) {
  return tags.join(',');  
};

/**
 * Setters
 */

var setTags = function (tags) {
  return tags.split(',');
};

/**
 * Article Schema
 */



var SectionTypeSchema = new Schema({
  sectionType: { type: String, default: '', trim : true },
});

/**
 * Validations
 */

SectionTypeSchema.path('sectionType').required(true, 'Section tipi boş olamaz');



/**
 * Pre-remove hook
 */

SectionTypeSchema.pre('remove', function (next) {
  var imager = new Imager(imagerConfig, 'S3');
  var files = this.image.files;

  // if there are files associated with the item, remove from the cloud too
  imager.remove(files, function (err) {
    if (err) return next(err);
  }, 'sectionType');

  next();
});

/**
 * Methods
 */

SectionTypeSchema.pre('save', function(next) {
  next();
});


SectionTypeSchema.methods = {

  /**
   * Save article and upload image
   *
   * @param {Object} images
   * @param {Function} cb
   * @api private
   */

  canSeeByUser: function(user){
    return true;
  },
  canEditByUser: function(user) {
    
      return true;
  },

  
};

/**
 * Statics
 */
SectionTypeSchema.statics = {

  /**
   * Find article by id
   *
   * @param {ObjectId} id
   * @param {Function} cb
   * @api private
   */
load: function (id, cb) {
    this.findOne({ _id : id })
      .populate('user', 'name email username')
      .populate('comments.user')
      .exec(cb);
  },

  /**
   * List articles
   *
   * @param {Object} options
   * @param {Function} cb
   * @api private
   */

  list: function (options, cb) {
    var criteria = options.criteria || {}

    this.find(criteria)
      .populate('user', 'name username')
      .sort({'createdAt': -1}) // sort by date
      .limit(options.perPage)
      .skip(options.perPage * options.page)
      .exec(cb);
  }
  
}

var SectionType = mongoose.model('SectionType', SectionTypeSchema);
