import mongoose from "mongoose"; const notificationSchema = new mongoose.Schema( { tenant: { type: mongoose.Schema.Types.ObjectId, ref: "Tenant", required: true, index: true, }, invoice: { type: mongoose.Schema.Types.ObjectId, ref: "Invoice", required: true, }, customer: { type: mongoose.Schema.Types.ObjectId, ref: "Customer", default: null, }, // Phone number for WhatsApp (may not be linked to customer) phoneNumber: { type: String, required: true, trim: true, }, notificationType: { type: String, enum: [ "order-confirmed", "pending", "preparing", "ready_for_delivery", // ✅ match invoiceController status strings "shipped", "out_for_delivery", // ✅ underscore consistency "delivered", "dine-in-ready", // ✅ for dine-in table-ready notifications ], required: true, }, messageTemplate: { type: String, required: true, }, channel: { type: String, enum: ["whatsapp", "sms", "email"], default: "whatsapp", }, status: { type: String, enum: ["queued", "sent", "delivered", "failed"], default: "queued", index: true, }, retryCount: { type: Number, default: 0, }, maxRetries: { type: Number, default: 3, }, externalId: { type: String, default: null, }, errorMessage: { type: String, default: null, }, sentAt: { type: Date, default: null, }, scheduledFor: { type: Date, default: null, }, }, { timestamps: true, } ); // Indexes for performance optimization notificationSchema.index({ tenant: 1, status: 1 }); notificationSchema.index({ tenant: 1, order: 1 }); notificationSchema.index({ status: 1, scheduledFor: 1 }); const Notification = mongoose.model("Notification", notificationSchema); export default Notification;