const mongoose = require('mongoose');
const { tenantFields, moneySchema, applyPlatformPlugins } = require('../platformFields');

const schema = new mongoose.Schema({
  ...tenantFields,
  branch: { type: mongoose.Schema.ObjectId, ref: 'Branch', autopopulate: true },
  product: { type: mongoose.Schema.ObjectId, ref: 'Product', required: true, autopopulate: true },
  type: { type: String, enum: ['sale', 'purchase', 'adjustment'], required: true },
  quantity: { type: Number, required: true },
  beforeQuantity: Number,
  afterQuantity: Number,
  referenceType: String,
  referenceId: mongoose.Schema.ObjectId,
  value: moneySchema,
  notes: String,
});

module.exports = mongoose.model('StockMovement', applyPlatformPlugins(schema));
