#!/usr/bin/env node

import * as fs from 'fs';
import * as path from 'path';
import { glob } from 'glob';

class FrontmatterFixerFinal {
  private docsRoot: string;

  constructor(docsRoot: string) {
    this.docsRoot = docsRoot;
  }

  async fixAllFrontmatter(): Promise<void> {
    console.log('🔧 Correction finale des frontmatter YAML...');

    const patterns = [
      'current/**/*.md',
      'vision/**/*.md'
    ];

    const allFiles: string[] = [];
    
    for (const pattern of patterns) {
      const files = await glob(pattern, { cwd: this.docsRoot });
      allFiles.push(...files);
    }

    console.log(`📄 ${allFiles.length} fichiers à vérifier`);

    let fixedCount = 0;

    for (const file of allFiles) {
      const fullPath = path.join(this.docsRoot, file);
      const content = fs.readFileSync(fullPath, 'utf-8');
      
      const fixedContent = this.fixFrontmatter(content);
      
      if (fixedContent !== content) {
        fs.writeFileSync(fullPath, fixedContent);
        console.log(`✅ Corrigé: ${file}`);
        fixedCount++;
      }
    }

    console.log(`🎉 ${fixedCount} fichiers corrigés!`);
  }

  private fixFrontmatter(content: string): string {
    const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n/;
    const match = content.match(frontmatterRegex);
    
    if (!match) return content;

    const frontmatterText = match[1];
    const lines = frontmatterText.split('\n');
    const fixedLines: string[] = [];

    for (const line of lines) {
      if (line.trim() === '') {
        fixedLines.push(line);
        continue;
      }

      const colonIndex = line.indexOf(':');
      if (colonIndex === -1) {
        fixedLines.push(line);
        continue;
      }

      const key = line.substring(0, colonIndex).trim();
      let value = line.substring(colonIndex + 1).trim();

      // Nettoyer tous les types de guillemets
      if (value.startsWith('""') && value.endsWith('""')) {
        value = value.slice(2, -2);
      } else if (value.startsWith("''") && value.endsWith("''")) {
        value = value.slice(2, -2);
      } else if (value.startsWith('"') && value.endsWith('"')) {
        value = value.slice(1, -1);
      } else if (value.startsWith("'") && value.endsWith("'")) {
        value = value.slice(1, -1);
      }

      // Toujours entourer de guillemets doubles pour éviter les problèmes YAML
      fixedLines.push(`${key}: "${value}"`);
    }

    const fixedFrontmatter = fixedLines.join('\n');
    return content.replace(frontmatterRegex, `---\n${fixedFrontmatter}\n---\n`);
  }

  async run(): Promise<void> {
    console.log('🔧 Démarrage de la correction finale des frontmatter...');
    
    try {
      await this.fixAllFrontmatter();
      console.log('✅ Correction finale des frontmatter terminée!');
    } catch (error) {
      console.error('❌ Erreur lors de la correction des frontmatter:', error);
      process.exit(1);
    }
  }
}

// Exécution du script
if (require.main === module) {
  const docsRoot = process.argv[2] || path.join(__dirname, '..');
  const fixer = new FrontmatterFixerFinal(docsRoot);
  fixer.run();
}

export { FrontmatterFixerFinal };
