const Joi = require('joi');

const leadSchema = Joi.object({
  firstName: Joi.string().trim().min(2).max(50).required()
    .messages({
      'string.empty': 'First name is required',
      'string.min': 'First name must be at least 2 characters long',
      'string.max': 'First name cannot exceed 50 characters'
    }),
  lastName: Joi.string().trim().min(2).max(50).required()
    .messages({
      'string.empty': 'Last name is required',
      'string.min': 'Last name must be at least 2 characters long',
      'string.max': 'Last name cannot exceed 50 characters'
    }),
  email: Joi.string().email().required()
    .messages({
      'string.email': 'Please provide a valid email address',
      'string.empty': 'Email is required'
    }),
  phone: Joi.string().pattern(/^\+?[1-9]\d{1,14}$/).allow(null, '')
    .messages({
      'string.pattern.base': 'Please provide a valid phone number'
    }),
  status: Joi.string().valid('new', 'contacted', 'qualified', 'lost')
    .default('new'),
  source: Joi.string().max(100),
  notes: Joi.string().max(1000)
});

const querySchema = Joi.object({
  page: Joi.number().integer().min(1).default(1),
  limit: Joi.number().integer().min(1).max(100).default(10),
  status: Joi.string().valid('new', 'contacted', 'qualified', 'lost'),
  source: Joi.string(),
  sortBy: Joi.string().valid('createdAt', 'updatedAt', 'firstName', 'lastName', 'email'),
  sortOrder: Joi.string().valid('ASC', 'DESC')
});

module.exports = {
  validateLead: (lead) => leadSchema.validate(lead, { abortEarly: false }),
  validateQuery: (query) => querySchema.validate(query, { abortEarly: false })
};