const mongoose = require('mongoose');

const webhookSchema = new mongoose.Schema({
    eventType: {
        type: String,
        required: true,
        enum: [
            'booking_confirmation',
            'payment_received',
            'driver_assigned',
            'driver_arrived',
            'trip_started',
            'trip_completed',
            'booking_cancelled',
            'payment_failed',
            'refund_processed',
            'otp_verification'
        ],
        description: 'The event that triggers this webhook'
    },
    url: {
        type: String,
        required: true,
        trim: true,
        description: 'The callback URL where data will be posted'
    },
    description: {
        type: String,
        trim: true
    },
    isActive: {
        type: Boolean,
        default: true
    },
    secret: {
        type: String,
        description: 'Optional secret for verification'
    },
    lastTriggeredAt: {
        type: Date
    },
    failureCount: {
        type: Number,
        default: 0
    }
}, { timestamps: true });

// Index for faster lookups by eventType
webhookSchema.index({ eventType: 1, isActive: 1 });

module.exports = mongoose.model('Webhook', webhookSchema);
