/**
 * Environment Configuration
 */

import { config } from 'dotenv';

// Load .env file
config();

interface EnvConfig {
  PORT: number;
  NODE_ENV: 'development' | 'production' | 'test';
  ALLOWED_ORIGINS: string[];
  
  // Redis/Valkey
  REDIS_HOST: string;
  REDIS_PORT: number;
  REDIS_PASSWORD: string;
  
  // PocketBase
  POCKETBASE_URL: string;
  PB_COLLECTION: string;
  PB_AUTH_COLLECTION: string;
  POCKETBASE_ADMIN_EMAIL: string;
  POCKETBASE_ADMIN_PASSWORD: string;
  
  // Microsoft OAuth
  MICROSOFT_CLIENT_ID: string;
  MICROSOFT_CLIENT_SECRET: string;
  MICROSOFT_TENANT: string;
  MICROSOFT_REDIRECT_URI: string;
  MS_GRAPH_TOKEN: string;
  
  // Microsoft Graph / SharePoint
  DRIVE_ID: string;
  ITEM_ID: string;
  EXCEL_TABLE_NAME: string;
  
  // Logging
  LOG_LEVEL: 'debug' | 'info' | 'warn' | 'error';
  LOG_FILE: string;
  ERROR_LOG_FILE: string;
  
  // Token settings
  TOKEN_REFRESH_INTERVAL_MS: number;
  TOKEN_EXPIRY_BUFFER_MS: number;
}

function getEnv<T extends string | number>(key: string, defaultValue: T): T {
  const value = process.env[key];
  if (value === undefined) return defaultValue;
  
  if (typeof defaultValue === 'number') {
    return parseInt(value, 10) as T;
  }
  return value as T;
}

function getEnvArray(key: string, defaultValue: string[]): string[] {
  const value = process.env[key];
  if (!value) return defaultValue;
  return value.split(',').map(s => s.trim());
}

export const env: EnvConfig = {
  PORT: getEnv('PORT', 3005),
  NODE_ENV: getEnv('NODE_ENV', 'development') as EnvConfig['NODE_ENV'],
  ALLOWED_ORIGINS: getEnvArray('ALLOWED_ORIGINS', ['http://localhost:3005', 'http://localhost:5173']),
  
  REDIS_HOST: getEnv('REDIS_HOST', 'localhost'),
  REDIS_PORT: getEnv('REDIS_PORT', 6379),
  REDIS_PASSWORD: getEnv('REDIS_PASSWORD', ''),
  
  POCKETBASE_URL: getEnv('POCKETBASE_URL', 'https://pocketbase.ccllc.pro'),
  PB_COLLECTION: getEnv('PB_COLLECTION', 'Job_Info_Prod'),
  PB_AUTH_COLLECTION: getEnv('PB_AUTH_COLLECTION', 'Users'),
  POCKETBASE_ADMIN_EMAIL: getEnv('POCKETBASE_ADMIN_EMAIL', ''),
  POCKETBASE_ADMIN_PASSWORD: getEnv('POCKETBASE_ADMIN_PASSWORD', ''),
  
  MICROSOFT_CLIENT_ID: getEnv('MICROSOFT_CLIENT_ID', ''),
  MICROSOFT_CLIENT_SECRET: getEnv('MICROSOFT_CLIENT_SECRET', ''),
  MICROSOFT_TENANT: getEnv('MICROSOFT_TENANT', '3fd97ea7-b124-41f1-855f-52d8ac3b16c7'),
  MICROSOFT_REDIRECT_URI: getEnv('MICROSOFT_REDIRECT_URI', 'https://pocketbase.ccllc.pro/api/oauth2-redirect'),
  MS_GRAPH_TOKEN: getEnv('MS_GRAPH_TOKEN', ''),
  
  DRIVE_ID: getEnv('DRIVE_ID', ''),
  ITEM_ID: getEnv('ITEM_ID', ''),
  EXCEL_TABLE_NAME: getEnv('EXCEL_TABLE_NAME', 'Job_List'),
  
  LOG_LEVEL: getEnv('LOG_LEVEL', 'info') as EnvConfig['LOG_LEVEL'],
  LOG_FILE: getEnv('LOG_FILE', 'logs/app.log'),
  ERROR_LOG_FILE: getEnv('ERROR_LOG_FILE', 'logs/error.log'),
  
  TOKEN_REFRESH_INTERVAL_MS: getEnv('TOKEN_REFRESH_INTERVAL_MS', 1800000),
  TOKEN_EXPIRY_BUFFER_MS: getEnv('TOKEN_EXPIRY_BUFFER_MS', 900000),
};

export function validateEnv(): void {
  const required = [
    'POCKETBASE_URL',
    'MICROSOFT_CLIENT_ID',
    'MICROSOFT_CLIENT_SECRET',
    'MICROSOFT_TENANT',
  ];
  
  const missing = required.filter(key => !process.env[key]);
  
  if (missing.length > 0) {
    console.warn(`Warning: Missing environment variables: ${missing.join(', ')}`);
  }
}
