import mongoose from 'mongoose';

const heldBillItemSchema = new mongoose.Schema(
  {
    productId: { type: mongoose.Schema.Types.ObjectId, required: true },
    productName: { type: String, default: '' },
    image: { type: String, default: '' },
    variantId: { type: String, default: '' },
    variantLabel: { type: String, default: '' },
    sellingUnit: { type: String, default: 'pcs' },
    size: { type: String, default: '' },
    measurementValue: { type: Number, default: null },
    measurementUnit: { type: String, default: '' },
    price: { type: Number, required: true, min: 0 },
    quantity: { type: Number, required: true, min: 1 },
  },
  { _id: false }
);

const heldBillSchema = new mongoose.Schema(
  {
    holdNumber: { type: String, required: true },
    items: { type: [heldBillItemSchema], required: true },
    customerName: { type: String, default: '', trim: true, maxlength: 120 },
    customerPhone: { type: String, default: '', trim: true, maxlength: 20 },
    status: { type: String, enum: ['HELD', 'RESUMED', 'DELETED'], default: 'HELD' },
    createdBy: {
      id: { type: mongoose.Schema.Types.ObjectId, ref: 'Admin' },
      name: { type: String, default: '' },
    },
  },
  { timestamps: true }
);

export default mongoose.model('HeldBill', heldBillSchema);
