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

'use strict';

const path = require('path');

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const { RESOURCE_TYPE_LIST } = require(path.join(__dirname, 'lib', 'resource-stats.js'));

const ResourceViewSchema = new Schema({
  created: { type: Date, default: Date.now, required: true, index: 1, expires: '30d' },
  resourceType: { type: String, enum: RESOURCE_TYPE_LIST, required: true },
  resource: { type: Schema.ObjectId, required: true, index: 1, refPath: 'resourceType' },
  uniqueKey: { type: String, required: true, index: 1 },
  visitCount: { type: Number, default: 0, required: true },
});

ResourceViewSchema.index({
  created: 1,
  resourceType: 1,
  resource: 1,
  uniqueKey: 1,
}, {
  name: 'res_view_daily_unique',
});

module.exports = mongoose.model('ResourceView', ResourceViewSchema);