const mongoose = require("mongoose")

const stockSchema = new mongoose.Schema(
  {
    /* LINK TO VEHICLE (Already Correct) */
    vehicleId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "CommercialVehicle",
      required: false,
      // index: true,
    },

    /*  SNAPSHOT */
    vehicleNumber: String,

    materialDetails: {
      poNumber: String,
      supplierName: String,
      materialType: String,
      materialName: String,
      quantityUnit: String,
    },

    /*  MASTER REFERENCES */
    productId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Product",
      index: true,
    },

    warehouseId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Warehouse",
      index: true,
    },

    /*  USERS */
    receivedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },

    qcBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },

    /*  STOCK VALUES */
    grossWeight: Number, // vehicle.inWeight snapshot
    tareWeight: Number, // vehicle.outWeight snapshot

    netQuantity: {
      type: Number, // final stock qty
      required: true,
      
    },
    receivedDate: {
      type: Date,
      default: Date.now,
    },

    referenceNo: String,

    remarks: String,

    isDeleted: {
      type: Boolean,
      default: false,
    },
    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },
  },
  { timestamps: true },
);


module.exports = mongoose.model("Stock", stockSchema);