import mongoose from "mongoose"


const couponSchema = new mongoose.Schema({
    code: { type: String, required: true, unique: true },
    discountType: { 
        type: String, 
        enum: ['percentage', 'free'], 
        required: true 
    }, // 'percentage' for % off, 'free' for 100% off
    discountAmount: { type: Number, required: true }, // Percentage value (e.g., 70 for 70% off, 100 for free)
    serviceType: { 
        type: [String], 
        enum: ['websiteSubmission', 'directory', 'jobPost', 'aiWebsite'], 
        required: true 
    }, // Which services this coupon applies to
    validFrom: { type: Date, default: Date.now }, // Start date for coupon validity
    validUntil: { type: Date, required: true }, 
    maxUsage: { type: Number, required: true }, // Global max usage limit
    usageCount: { type: Number, default: 0 }, // Current global usage count
    isActive: { type: Boolean, default: true }, // Whether the coupon is active or not
    usedBy: [{ 
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'User' 
    }], // Array of user IDs who have used this coupon
});
// Optional: Add custom validation to check if validUntil is after validFrom
couponSchema.pre('save', function(next) {
    if (this.validUntil <= this.validFrom) {
        return next(new Error('validUntil must be later than validFrom.'));
    }
    next();
});

const Coupon = mongoose.model("Coupon", couponSchema);
export default Coupon;