import dotenv from 'dotenv';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export function loadConfig() {
    // Load .env — prefer DOTENV_PATH (set by CLI), fallback to project root
    const projectRoot = path.resolve(__dirname, '..', '..');
    const dotenvFile = process.env.DOTENV_PATH ?? path.join(projectRoot, '.env');
    dotenv.config({ path: dotenvFile });
    const jwtSecret = process.env.JWT_SECRET;
    if (!jwtSecret || jwtSecret.length < 32) {
        throw new Error('JWT_SECRET environment variable is required and must be at least 32 characters. ' +
            'Run "npx expxagents init" to generate one.');
    }
    const port = parseInt(process.env.PORT || '3001', 10);
    const corsOrigin = process.env.CORS_ORIGIN || `http://localhost:${port}`;
    const nodeEnv = process.env.NODE_ENV || 'development';
    // DATA_DIR must be set by CLI in production; fallback uses cwd for dev
    const dataDir = process.env.DATA_DIR ?? path.join(process.cwd(), '.expxagents', 'data');
    const squadsDir = process.env.SQUADS_DIR ?? path.resolve(projectRoot, 'squads');
    return {
        port,
        corsOrigin,
        nodeEnv,
        jwtSecret,
        dataDir,
        squadsDir,
        awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID || undefined,
        awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || undefined,
        awsRegion: process.env.AWS_REGION || 'us-east-1',
        sesFromEmail: process.env.SES_FROM_EMAIL || '',
        sesRateLimit: parseInt(process.env.SES_RATE_LIMIT || '14', 10),
        serverUrl: process.env.SERVER_URL || `http://localhost:${port}`,
        knowledgeWatchPaths: process.env.KNOWLEDGE_WATCH_PATHS
            ? process.env.KNOWLEDGE_WATCH_PATHS.split(',').map(p => path.resolve(p.trim()))
            : [],
    };
}
//# sourceMappingURL=config.js.map