import { Schema, model, models } from 'mongoose';
import { QuestionData as IQusetion } from '../utils/interface/questionInterface';

const validator = require('validator');

const QuestionSchema = new Schema<IQusetion>({
  firstName: {
    type: String,
    required: [true, 'Please provide a name.'],
    maxlength: [60, 'Name cannot be more than 60 characters'],
    trim: true,
  },
  lastName: {
    type: String,
    required: [true, 'Please provide a last name.'],
    maxlength: [60, 'Name cannot be more than 60 characters'],
    trim: true,
  },
  email: {
    type: String,
    required: [true, 'Please provide an email.'],
    trim: true,
    lowercase: true,
    unique: false,
    validate(value: string) {
      if (!validator.isEmail(value)) {
        throw new Error('Email is invalid');
      }
    },
  },
  message: {
    type: String,
    required: [true, 'Please provide a message/question.'],
    trim: true,
  },
});

const Question =
  models.Question || model<IQusetion>('Question', QuestionSchema, 'Questions');
module.exports = Question;
