import Joi from 'joi';
import IdValidate from './id';

export default class RootValidate {  // validate input for root methods
  private static changelog = {
    timestamp: Joi.date()
      .iso()
      .min('1970-01-01T00:00:00.000Z'),

    page: Joi.number()
      .integer()
      .min(0)
      .default(0),

    pagesize: Joi.number()
      .integer()
      .min(0)
      .default(25),

    action: Joi.string(),

    collection: Joi.string(),

    conditions: Joi.object(),

    data: Joi.object()
  };

  static changelogParams (data) {
    return Joi.object({
      timestamp: this.changelog.timestamp.required(),
      page: this.changelog.page,
      pagesize: this.changelog.pagesize
    }).validate(data);
  }

  static changelogOutput (data) {
    data.date = data._id.getTimestamp();
    data.collection = data.collection_name;
    data = IdValidate.stringify(data);
    const {value, error} = Joi.object({
      date: this.changelog.timestamp,
      action: this.changelog.action,
      collection: this.changelog.collection,
      conditions: this.changelog.conditions,
      data: this.changelog.data,
    }).validate(data, {stripUnknown: true});
    return error !== undefined? null : value;
  }
}