const BaseState = require('@abtnode/core/lib/states/base');
const logger = require('@abtnode/logger')('@abtnode/auth-service:states:message');

class MessageState extends BaseState {
  constructor(baseDir, options = {}) {
    super(baseDir, { filename: 'message.db', ...options });

    this.db.ensureIndex({ fieldName: 'did' }, (error) => {
      if (error) {
        logger.error('ensure index failed', { error });
      }
    });
  }

  insert(doc, ...args) {
    return super.insert(
      {
        ...doc,
        created: Date.now(),
        updated: Date.now(),
      },
      ...args
    );
  }

  prune(time) {
    const t = time || 30 * 24 * 60 * 60 * 1000; // 30 day
    return this.remove({ created: { $lt: Date.now() - t } });
  }
}

module.exports = MessageState;
