'use strict';

const path = require('node:path');
try {
  require('dotenv').config({ path: path.resolve(process.cwd(), '.env') });
} catch (_) {
  // Allows syntax/unit checks before dependencies are installed.
}

function intEnv(name, fallback) {
  const value = Number.parseInt(process.env[name] || '', 10);
  return Number.isFinite(value) ? value : fallback;
}

const rootDir = path.resolve(__dirname, '..');

module.exports = {
  rootDir,
  port: intEnv('PORT', 3000),
  env: process.env.NODE_ENV || 'development',
  appName: process.env.APP_NAME || 'نظام الخضار',
  timezone: process.env.TZ || 'Asia/Riyadh',
  db: {
    host: process.env.DB_HOST || '127.0.0.1',
    port: intEnv('DB_PORT', 3306),
    name: process.env.DB_NAME || 'vegetable_system',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || '',
    connectionLimit: intEnv('DB_CONNECTION_LIMIT', 10),
  },
  backupDir: path.resolve(rootDir, process.env.BACKUP_DIR || 'storage/backups'),
  backupRetentionDays: intEnv('BACKUP_RETENTION_DAYS', 180),
  isProduction: (process.env.NODE_ENV || 'development') === 'production',
};
