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

// Define the main partStock schema
const partStockSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    state: {
      type: { type: String, required: true },
      progress: { type: Number, required: false },
    },
    part: { type: mongoose.Schema.Types.ObjectId, ref: 'part', required: true },
    startingQuantity: { type: Number, required: true },
    currentQuantity: { type: Number, required: true },
  },
  { timestamps: true }
);

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

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

// Create and export the model
export const partStockModel = mongoose.model('partStock', partStockSchema);
