#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

const EXPORTS_DIR = path.join(__dirname, '../claude-exports-last-3-days');

/**
 * Parse un fichier de session pour extraire les tool uses
 */
function parseSessionFile(filePath) {
    const content = fs.readFileSync(filePath, 'utf-8');
    const tools = [];

    // Chercher tous les blocs JSON qui contiennent des tool uses
    const jsonBlockRegex = /\[\s*\{[\s\S]*?"type":\s*"tool_use"[\s\S]*?\}\s*\]/g;
    const matches = content.match(jsonBlockRegex);

    if (!matches) return tools;

    for (const match of matches) {
        try {
            const parsed = JSON.parse(match);
            for (const item of parsed) {
                if (item.type === 'tool_use' && (item.name === 'Edit' || item.name === 'Write')) {
                    tools.push({
                        name: item.name,
                        input: item.input
                    });
                }
            }
        } catch (e) {
            // Skip invalid JSON
        }
    }

    return tools;
}

/**
 * Applique un Edit sur un fichier
 */
function applyEdit(filePath, oldString, newString) {
    try {
        if (!fs.existsSync(filePath)) {
            console.log(`⏭️  SKIP Edit - Fichier n'existe pas: ${filePath}`);
            return false;
        }

        const content = fs.readFileSync(filePath, 'utf-8');

        if (!content.includes(oldString)) {
            console.log(`⏭️  SKIP Edit - old_string non trouvée dans: ${filePath}`);
            return false;
        }

        const newContent = content.replace(oldString, newString);
        fs.writeFileSync(filePath, newContent, 'utf-8');
        console.log(`✅ EDIT appliqué: ${filePath}`);
        return true;
    } catch (e) {
        console.log(`❌ ERREUR Edit sur ${filePath}: ${e.message}`);
        return false;
    }
}

/**
 * Applique un Write sur un fichier
 */
function applyWrite(filePath, content) {
    try {
        if (fs.existsSync(filePath)) {
            console.log(`⏭️  SKIP Write - Fichier existe déjà: ${filePath}`);
            return false;
        }

        // Créer les dossiers parents si nécessaire
        const dir = path.dirname(filePath);
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }

        fs.writeFileSync(filePath, content, 'utf-8');
        console.log(`✅ WRITE appliqué: ${filePath}`);
        return true;
    } catch (e) {
        console.log(`❌ ERREUR Write sur ${filePath}: ${e.message}`);
        return false;
    }
}

/**
 * Main
 */
function main() {
    console.log('🔄 Application des exports Claude...\n');

    // Lire tous les fichiers de session
    const sessionFiles = fs.readdirSync(EXPORTS_DIR)
        .filter(f => f.endsWith('-session.md'))
        .sort((a, b) => {
            const numA = parseInt(a.split('-')[0]);
            const numB = parseInt(b.split('-')[0]);
            return numB - numA; // Ordre inverse: 15 -> 1
        });

    console.log(`📁 ${sessionFiles.length} fichiers de session trouvés`);
    console.log(`📋 Ordre de traitement: ${sessionFiles.join(', ')}\n`);

    let totalEdits = 0;
    let totalWrites = 0;
    let successEdits = 0;
    let successWrites = 0;

    for (const sessionFile of sessionFiles) {
        const filePath = path.join(EXPORTS_DIR, sessionFile);
        console.log(`\n📄 Traitement de: ${sessionFile}`);

        const tools = parseSessionFile(filePath);
        console.log(`   ${tools.length} tool use(s) trouvé(s)`);

        for (const tool of tools) {
            if (tool.name === 'Edit') {
                totalEdits++;
                const { file_path, old_string, new_string } = tool.input;
                if (applyEdit(file_path, old_string, new_string)) {
                    successEdits++;
                }
            } else if (tool.name === 'Write') {
                totalWrites++;
                const { file_path, content } = tool.input;
                if (applyWrite(file_path, content)) {
                    successWrites++;
                }
            }
        }
    }

    console.log('\n\n📊 RÉSUMÉ:');
    console.log(`   Edit:  ${successEdits}/${totalEdits} appliqués`);
    console.log(`   Write: ${successWrites}/${totalWrites} appliqués`);
    console.log(`   Total: ${successEdits + successWrites}/${totalEdits + totalWrites} opérations réussies`);
    console.log('\n✨ Terminé!');
}

main();
