const mongoose = require('mongoose');

const libraryViewSchema = new mongoose.Schema(
  {
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true
    },
    companyId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Company',
      required: true
    },
    lastViewedAt: {
      type: Date,
      default: Date.now
    }
  },
  {
    timestamps: true
  }
);

// Compound index for fast lookup
libraryViewSchema.index({ userId: 1, companyId: 1 }, { unique: true });

module.exports = mongoose.model('LibraryView', libraryViewSchema);
