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

const schema = new mongoose.Schema({
  ...tenantFields,
  branch: { type: mongoose.Schema.ObjectId, ref: 'Branch', required: true, autopopulate: true },
  product: { type: mongoose.Schema.ObjectId, ref: 'Product', required: true, autopopulate: true },
  onHand: { type: Number, default: 0 },
  reserved: { type: Number, default: 0 },
  reorderLevel: { type: Number, default: 0 },
});

schema.index({ tenant: 1, branch: 1, product: 1 }, { unique: true });

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