import mongoose, { Schema } from "mongoose";

const inventorySchema = new Schema({
    customId: String,
    name: String,
    purchasePrice: Number,
    totalSell: Number,
    sellPrice: Number,
    quantity: Number,
    stockPrice:Number,
    userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true,
    }
}, { timestamps: true })
inventorySchema.pre('save', async function () {
    if (!this.customId) {
        let isUnique = false;
        let newId;

        while (!isUnique) {
            newId = Math.floor(100000 + Math.random() * 900000).toString(); // ✅ 6-digit

            const existing = await this.constructor.findOne({ customId: newId });
            if (!existing) {
                isUnique = true;
            }
        }

        this.customId = newId;
    }
});
const Inventory = mongoose.model('Inventory', inventorySchema);
export default Inventory;