const joi = require('joi');

exports.validateEnv = () => {
  const envSchema = joi.object({
    NODE_ENV: joi.string().valid('development', 'production', 'test').required(),
    PORT: joi.number().default(3000),
    DB_HOST: joi.string().required(),
    DB_USER: joi.string().required(),
    DB_PASSWORD: joi.string().required(),
    DB_NAME: joi.string().required(),
    JWT_SECRET: joi.string().required().min(32),
    JWT_EXPIRES_IN: joi.string().required(),
    ALLOWED_ORIGINS: joi.string(),
    LOG_LEVEL: joi.string().valid('error', 'warn', 'info', 'debug').default('info'),
    RATE_LIMIT_WINDOW_MS: joi.number(),
    RATE_LIMIT_MAX_REQUESTS: joi.number()
  }).unknown();

  const { error } = envSchema.validate(process.env);
  if (error) {
    throw new Error(`Environment validation failed: ${error.message}`);
  }
}