import dotenv from 'dotenv';
import path from 'path';

dotenv.config();

export const config = {
  env: process.env.NODE_ENV || 'development',
  port: parseInt(process.env.PORT || '5001', 10),
  apiVersion: process.env.API_VERSION || 'v1',
  
  database: {
    url: process.env.DATABASE_URL || '',
  },
  
  jwt: {
    secret: process.env.JWT_SECRET || 'change-this-secret',
    expiresIn: process.env.JWT_EXPIRES_IN || '7d',
    refreshExpiresIn: process.env.JWT_REFRESH_EXPIRES_IN || '30d',
  },
  
  cors: {
    origin: (() => {
      const envOrigins = process.env.CORS_ORIGIN?.split(',').map((o) => o.trim()).filter(Boolean);
      const defaults = ['http://localhost:3000', 'http://localhost:5173'];
      const origins = envOrigins?.length ? envOrigins : defaults;
      // In development, always allow both common dev server origins
      if (process.env.NODE_ENV !== 'production') {
        ['http://localhost:3000', 'http://localhost:5173'].forEach((o) => {
          if (!origins.includes(o)) origins.push(o);
        });
      }
      return origins;
    })(),
  },
  
  upload: {
    maxFileSize: parseInt(process.env.MAX_FILE_SIZE || '104857600', 10),
    path: process.env.UPLOAD_PATH || path.resolve(process.cwd(), 'uploads'),
  },
    
  pagination: {
    defaultPageSize: parseInt(process.env.DEFAULT_PAGE_SIZE || '20', 10),
    maxPageSize: parseInt(process.env.MAX_PAGE_SIZE || '100', 10),
  },
  
  audit: {
    retentionDays: parseInt(process.env.AUDIT_LOG_RETENTION_DAYS || '2555', 10), // ~7 years
  },
  
  security: {
    bcryptRounds: parseInt(process.env.BCRYPT_ROUNDS || '10', 10),
    rateLimitWindowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS || '900000', 10), // 15 min
    rateLimitMaxRequests: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS || '100', 10),
  },
};

