/**
 * Configuration loader
 * Must be imported first to load environment variables
 */

import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';

// Get __dirname equivalent in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Load environment variables from root directory
dotenv.config({ path: path.resolve(__dirname, '../.env') });

// Export config values
export const config = {
  // Server
  port: parseInt(process.env.TMS_PORT || process.env.PORT || '4000', 10),
  
  // TMS API
  tmsApiUrl: process.env.TMS_API_URL || 'https://tms.kiledel.com',
  tmsToken: process.env.TMS_TOKEN || '',
  tmsEmail: process.env.TMS_EMAIL || '',
  tmsPassword: process.env.TMS_PASSWORD || '',
  
  // MongoDB
  mongodbUri: process.env.MONGODB_URI || '',
  mongodbDatabase: process.env.MONGODB_DATABASE || '',
  
  // Supabase
  supabaseUrl: process.env.SUPABASE_URL || '',
  supabaseServiceRoleKey: process.env.SUPABASE_SERVICE_ROLE_KEY || '',
  
  // Worker
  workerIntervalMs: parseInt(process.env.WORKER_INTERVAL_MS || '3000', 10),
  workerBatchSize: parseInt(process.env.WORKER_BATCH_SIZE || '10', 10),
  maxRetries: parseInt(process.env.MAX_RETRIES || '3', 10),
  requestDelayMs: parseInt(process.env.REQUEST_DELAY_MS || '100', 10),
  
  // Submission defaults
  defaultPerms: process.env.DEFAULT_PERMS || '',
  defaultPermsFinal: process.env.DEFAULT_PERMS_FINAL || '',
};
