/**
 * env.ts
 * Lee y valida las variables de entorno.
 * Centraliza toda la configuración que viene del .env
 */

const getEnvVar = (key: string, defaultValue?: string): string => {
  const value = process.env[key];
  if (!value && defaultValue === undefined) {
    throw new Error(`Missing environment variable: ${key}`);
  }
  return (value as string) || (defaultValue as string);
};

const requiredEnvVar = (key: string): string => {
  const value = process.env[key];
  if (!value) {
    throw new Error(`Missing required environment variable: ${key}`);
  }
  return value;
};

export const env = {
  // Server
  PORT: Number(getEnvVar("PORT", "3000")),
  NODE_ENV: getEnvVar("NODE_ENV", "development"),

  // Database
  DATABASE_URL: requiredEnvVar("DATABASE_URL"),

  // JWT
  JWT_SEED: requiredEnvVar("JWT_SEED"),
  JWT_EXPIRES_IN: getEnvVar("JWT_EXPIRES_IN", "7d"),

  // Bcrypt
  BCRYPT_ROUNDS: Number(getEnvVar("BCRYPT_ROUNDS", "10")),
} as const;

