const mongoose = require('mongoose');

const itemSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
    },
    code: {
      type: String,
      required: true,
    },
    avg_price: {
      type: Number,
      required: true,
    },
    selling_price: {
      type: Number,
      required: true,
    },
    quantity: {
      type: Number,
      required: true,
      default: 0, // Default value for stock quantity
    },
    companyId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Company',
      required: true,
    },


    kitchenId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Kitchen',
      required: false,
    },

    categoryId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Catagory',
      required: false,
    },

    defaultItemType: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'DefaultItemType',
      required: false,
    },
    attachmentReq: { 
      type: Boolean,default:false,
    },



    status: {
      type: String,
      enum: ['active', 'inactive'],
      default: 'active', // Set default status
    },
    type: {
      type: String,
      enum: ['store', 'shop','attachment'],
      default: 'shop', // Set default shop
    },

  
    needStock: { 
      type: Boolean,default:false
    },
    
      image_url: { 
      type: String 
    },
  },
  { timestamps: true }
);

// Create compound unique index on 'code' and 'companyId'
itemSchema.index({ code: 1, companyId: 1 }, { unique: true });

module.exports = mongoose.model('Item', itemSchema);


// { $push: { expDates: { expDate: new Date('2024-04-10'), createdDate: Date.now() } } },





// item = await Item.create({
//   _id: itemId, // Important: if you want to set the _id manually
//   name: "Item Name", // Example data
//   description: "Item Description", // Example data
//   quantity: 0,
//   expDates: [newExpDate], // Initialize expDates with the first entry
//   totalQuantity: 0
// });
// console.log("item created")
// } else {
// if (desiredIndex === undefined || item.expDates.length === 0) {
//   // Item exists, add a new expDate using $push
//   item = await Item.findByIdAndUpdate(
//     itemId,
//     { $push: { expDates: newExpDate } },
//     { new: true, runValidators: true }
//   );
// } 