// log.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const mongoose = require('mongoose');

const Log = mongoose.model('Log');

const { SiteService } = require('../../lib/site-lib');

class SystemLogService extends SiteService {

  constructor (dtp) {
    super(dtp, module.exports);
  }

  async getRecords (search, pagination) {
    const logs = await Log
      .find(search)
      .sort({ created: -1 })
      .skip(pagination.skip)
      .limit(pagination.cpp)
      .lean();
    return logs;
  }

  async getComponentSlugs ( ) {
    return await Log.distinct('component.slug');
  }

  async getTotalCount ( ) {
    const count = await Log.estimatedDocumentCount();
    this.log.debug('log message total count', { count });
    return count;
  }
}

module.exports = {
  slug: 'log',
  name: 'log',
  create: (dtp) => { return new SystemLogService(dtp); },
};