const mongoose = require('mongoose');

const printedDataSchema = new mongoose.Schema({
  orderId: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'CafeOrder', 
    required: true 
  },
  userId: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'User', 
    required: false 
  },
  orderNo: { 
    type: Number, 
    required: true 
  },
  tableNo: { 
    type: Number, 
    required: true 
  },
  waiterName: { 
    type: String, 
    required: false 
  },
  printType: {
    type: String,
    enum: ['new_order', 'order_update'],
    default: 'order_update'
  },
  addedItems: [{
    itemId: { 
      type: mongoose.Schema.Types.ObjectId, 
      ref: 'Item' 
    },
    itemName: { 
      type: String, 
      required: true 
    },
    quantity: { 
      type: Number, 
      required: true 
    },
    specialNeeds: { 
      type: [String], 
      default: [] 
    }
  }],
  quantityChanged: [{
    itemId: { 
      type: mongoose.Schema.Types.ObjectId, 
      ref: 'Item' 
    },
    itemName: { 
      type: String, 
      required: true 
    },
    previousQuantity: { 
      type: Number, 
      required: true 
    },
    currentQuantity: { 
      type: Number, 
      required: true 
    },
    quantityChange: { 
      type: Number, 
      required: true 
    },
    specialNeeds: { 
      type: [String], 
      default: [] 
    }
  }],
  removedItems: [{
    itemId: { 
      type: mongoose.Schema.Types.ObjectId, 
      ref: 'Item' 
    },
    itemName: { 
      type: String, 
      required: true 
    },
    quantity: { 
      type: Number, 
      required: true 
    },
    specialNeeds: { 
      type: [String], 
      default: [] 
    }
  }]
}, {
  timestamps: true // Adds createdAt and updatedAt automatically
});

module.exports = mongoose.model('PrintedData', printedDataSchema);

