import mongoose from 'mongoose'; const productSchema = new mongoose.Schema({ name: { type: String, required: true, trim: true }, description: { type: String, trim: true }, barcode: { type: String, unique: true, sparse: true, trim: true }, sku: { type: String, unique: true, sparse: true, trim: true }, category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true }, price: { type: Number, required: true, min: 0 }, currency: { type: String, default: 'TZS' }, // Stock management stockQuantity: { type: Number, default: 0, min: 0 }, lowStockThreshold: { type: Number, default: 5 }, inStock: { type: Boolean, default: false }, // Restock info (when out of stock) expectedRestockDate: { type: Date }, restockNote: { type: String }, // Images images: [{ url: { type: String }, key: { type: String }, // R2 object key isPrimary: { type: Boolean, default: false }, }], // Product details unit: { type: String, default: 'piece', enum: ['piece', 'kg', 'g', 'litre', 'ml', 'pack', 'dozen', 'bundle', 'bunch', 'tray', 'box', 'bottle', 'sachet', 'jar', 'can', 'bag'] }, brand: { type: String }, origin: { type: String }, weight: { type: Number }, // in grams // Flags isFeatured: { type: Boolean, default: false }, isActive: { type: Boolean, default: true }, isPreorderable: { type: Boolean, default: true }, // Stats totalSold: { type: Number, default: 0 }, viewCount: { type: Number, default: 0 }, preorderCount: { type: Number, default: 0 }, // Preorder waitlist preorderWaitlist: [{ customer: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, guestEmail: { type: String }, guestPhone: { type: String }, guestName: { type: String }, quantity: { type: Number, default: 1 }, addedAt: { type: Date, default: Date.now }, notified: { type: Boolean, default: false }, }], // Barcode scan history (for audit) lastScannedAt: { type: Date }, lastScannedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, }, { timestamps: true }); // Auto-set inStock based on stockQuantity productSchema.pre('save', function (next) { this.inStock = this.stockQuantity > 0; next(); }); productSchema.virtual('isLowStock').get(function () { return this.stockQuantity > 0 && this.stockQuantity <= this.lowStockThreshold; }); // Normalize field names for frontend compatibility productSchema.set('toObject', { virtuals: true }); productSchema.set('toJSON', { virtuals: true, transform(doc, ret) { // Alias fields to what the frontend expects ret.stockQty = ret.stockQuantity; ret.featured = ret.isFeatured; ret.allowPreorder = ret.isPreorderable; // Flatten images array to plain URL strings if (Array.isArray(ret.images)) { ret.images = ret.images.map(img => (typeof img === 'object' && img !== null ? img.url : img)).filter(Boolean); } return ret; }, }); // Indexes (unique:true on schema field covers the barcode index already) productSchema.index({ name: 'text', description: 'text', brand: 'text', barcode: 'text' }); productSchema.index({ category: 1, inStock: 1, isActive: 1 }); productSchema.index({ isFeatured: 1, isActive: 1 }); export default mongoose.model('Product', productSchema);