/**
 * Strip technical descriptions from cahier des charges:
 * Remove lines that are file paths, config references, or "Fichiers modifiés" lists.
 */
const fs = require('fs');
const path = require('path');

const baseDir = path.resolve(__dirname, '..');
const filePath = path.join(baseDir, 'docs', 'cahier des charges.md');
let content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
const out = [];

function isTechnicalLine(line) {
  const t = line.trim();
  if (!t) return false;
  if (/^[-*]?\s*`?web\/js\//.test(t) || /^[-*]?\s*`?server\//.test(t)) return true;
  if (/^\*\*Fichiers (modifiés|impactés|concernés)/i.test(t)) return true;
  if (/^-\s*`[^`]+\.(js|css|ts)`\s*[;,.]/.test(t)) return true;
  if (/^\*\*Config\*\* `config\.js`/.test(t)) return true;
  if (/^-\s*`web\/js\/[^`]+`\s*$/.test(t)) return true;
  if (/^`web\/js\/[^`]+`,?\s*$/.test(t)) return true;
  if (/^-\s*\*\*[a-z0-9-]+\.(js|ts|css)\*\*/.test(t) && t.length < 130) return true;
  if (/^\d+\.\s+\*\*[a-z-]+\.(js|ts)\*\*/.test(t)) return true;
  if (/^-\s*`[a-z0-9/-]+\.(js|css|ts)`\s*[,(]/.test(t)) return true;
  return false;
}

let i = 0;
while (i < lines.length) {
  const line = lines[i];
  if (isTechnicalLine(line)) {
    i++;
    continue;
  }
  if (/^\*\*Fichiers (modifiés|impactés)/i.test(line.trim())) {
    i++;
    while (i < lines.length && (lines[i].trim().startsWith('-') || lines[i].trim() === '')) i++;
    continue;
  }
  out.push(line);
  i++;
}

fs.writeFileSync(filePath, out.join('\n'));
console.log('Lines: ' + lines.length + ' -> ' + out.length);
