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

const stockEventSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    value: { type: Number, required: true },
    unit: { type: String, required: true },
    parent: {
      type: Schema.Types.ObjectId,
      refPath: 'parentType',
      required: true,
    },
    parentType: {
      type: String,
      required: true,
      enum: ['filamentStock', 'partStock', 'productStock'], // Add other models as needed
    },
    owner: {
      type: Schema.Types.ObjectId,
      refPath: 'ownerType',
      required: true,
    },
    ownerType: {
      type: String,
      required: true,
      enum: ['user', 'subJob', 'stockAudit'],
    },
    timestamp: { type: Date, default: Date.now },
  },
  { timestamps: true }
);

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

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

// Create and export the model
export const stockEventModel = mongoose.model('stockEvent', stockEventSchema);
