var mongoose = require('mongoose');

var saleProductSchema = new mongoose.Schema({
  productCode: { type: String, required: true },
  productName: { type: String, required: true },
  quantity: { type: Number, required: true, min: 1 },
  unitPrice: { type: Number, required: true, min: 0 },
});

var saleSchema = new mongoose.Schema({
  invoiceNumber: { type: String, required: true, unique: true, trim: true },
  salesDate: { type: Date, required: true },
  paymentMethod: { type: String, required: true, trim: true },
  totalAmountPaid: { type: Number, required: true, min: 0 },
  customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true },
  products: [saleProductSchema],
}, { timestamps: true });

module.exports = mongoose.model('Sale', saleSchema);
