import mongoose from 'mongoose';
import { generateId } from '../../utils.js';
const { Schema } = mongoose;

const auditLogSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    changes: {
      old: { type: Object, required: false },
      new: { type: Object, required: false },
    },
    operation: {
      type: String,
      required: true,
    },
    parent: {
      type: Schema.Types.ObjectId,
      refPath: 'parentType',
      required: true,
    },
    parentType: {
      type: String,
      required: true,
    },
    owner: {
      type: Schema.Types.ObjectId,
      refPath: 'ownerType',
      required: true,
    },
    ownerType: {
      type: String,
      required: true,
      enum: ['user', 'printer', 'host'],
    },
  },
  { timestamps: true }
);

// Add virtual id getter
auditLogSchema.virtual('id').get(function () {
  return this._id;
});

// Configure JSON serialization to include virtuals
auditLogSchema.set('toJSON', { virtuals: true });

// Create and export the model
export const auditLogModel = mongoose.model('auditLog', auditLogSchema);
