import assert from 'node:assert';
import fs from 'fs';
import path from 'path';

// ✅ CHARGEMENT .ENV SI PAS DÉJÀ FAIT
const envPath = path.join(process.cwd(), '.env');
if (fs.existsSync(envPath) && !process.env._ENV_LOADED_BY_ENV_HELPER) {
  const lines = fs.readFileSync(envPath, 'utf8').split(/\r?\n/);
  for (const line of lines) {
    const match = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/i);
    if (match && !process.env[match[1]]) {
      process.env[match[1]] = match[2].replace(/^"|"$/g, '');
    }
  }
  process.env._ENV_LOADED_BY_ENV_HELPER = 'true';
}

export function requireEnv(keys) {
  const missing = [];
  for (const k of keys) {
    if (!process.env[k] || String(process.env[k]).trim() === '') missing.push(k);
  }
  return { ok: missing.length === 0, missing };
}

export const DEFAULT_REQUIRED = [
  // adapte à ton projet
  'OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'GOOGLE_API_KEY',
  'MAX_COST_PER_ARTICLE', 'TRACE_PATH'
];
