const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ChallanSchema = new Schema({
  challanNumber: { type: String },   
  name: { type: String, required: true },
  address: { type: String, default: null },
  productDetails: [{
    productCode: { type: String, required: true },
    productName: { type: String, required: true },
    quantity: { type: Number, required: true }
  }],
  createdAt: { type: Date, default: Date.now },
  updatedAt: { type: Date, default: Date.now }
});

ChallanSchema.pre('save', function (next) {
  this.updatedAt = Date.now();
  next();
});

module.exports = mongoose.model("Challan", ChallanSchema);
