const mongoose = require('mongoose');
const { isEmail } = require('validator');

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true,
        match: [/^\S+@\S+\.\S+$/, 'Please enter a valid email address'] 
    },
    password: {
        type: String,
        required: true
    },
    role:{
        type: String, 
        enum: ['admin', 'user', 'owner'], 
        default: 'user'
    },
    resetOTP: String,  // store OTP temporarily
    resetOTPExpiry: Date, // OTP expiry time
    created_at:{
        type: Date,
        required: true,
        default: Date.now,
    }
    
});


// ✅ Prevent OverwriteModelError by reusing existing model if present
const User = mongoose.models.User || mongoose.model('User', userSchema);

module.exports = User;