const mongoose = require('mongoose');

const branchInventorySchema = new mongoose.Schema({
  companyId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Company',
    required: true,
  },
  branchId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Branch',
    required: true,
  },
  itemId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Item',
    required: true,
  },
  quantity: {
    type: Number,
    default: 0,
    required: true,
  },
}, { timestamps: true });

// Create a compound index for branchId and itemId to ensure uniqueness
branchInventorySchema.index({ branchId: 1, itemId: 1 }, { unique: true });

module.exports = mongoose.model('BranchInventory', branchInventorySchema);
