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

const logPath = 'C:/Users/BS/.gemini/antigravity/brain/393257b2-e812-421c-92f7-099f2362049e/.system_generated/logs/transcript.jsonl';

function safeParse(val) {
    if (typeof val === 'string') {
        try { return JSON.parse(val); } catch(e) { return val; }
    }
    return val;
}

async function extract() {
    const fileStream = fs.createReadStream(logPath);
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    // Initialize with app.js since app_final.js was copied from it
    let appFinal = '';
    if (fs.existsSync('d:/برنامج مخازن/app.js')) {
        appFinal = fs.readFileSync('d:/برنامج مخازن/app.js', 'utf8');
    }
    
    // index.html starts with its current or original state? 
    // Actually, let's just trace index.html from write_to_file
    let indexHtml = '';
    const cutoffTime = '2026-06-03T06:15:00Z'; // 9:15 AM local time

    for await (const line of rl) {
        try {
            const entry = JSON.parse(line);
            if (entry.created_at && entry.created_at > cutoffTime) break;

            if (entry.tool_calls) {
                for (const tool of entry.tool_calls) {
                    try {
                        const name = tool.name || (tool.function && tool.function.name);
                        let args = tool.args;
                        if (!args && tool.function && tool.function.arguments) {
                            args = JSON.parse(tool.function.arguments);
                        } else if (typeof args === 'string') {
                            args = JSON.parse(args);
                        }
                        if (!args) continue;

                        let targetFile = safeParse(args.TargetFile || args.target_file || '');
                        let codeContent = safeParse(args.CodeContent || args.code_content || '');
                        let targetContent = safeParse(args.TargetContent || args.target_content || '');
                        let replacementContent = safeParse(args.ReplacementContent || args.replacement_content || '');

                        if (name === 'write_to_file' || name === 'default_api:write_to_file') {
                            if (targetFile.endsWith('app_final.js')) appFinal = codeContent;
                            if (targetFile.endsWith('index.html')) indexHtml = codeContent;
                        }
                        
                        if (name === 'replace_file_content' || name === 'default_api:replace_file_content') {
                            if (targetFile.endsWith('app_final.js') && appFinal && appFinal.includes(targetContent)) {
                                appFinal = appFinal.replace(targetContent, replacementContent);
                            }
                            if (targetFile.endsWith('index.html') && indexHtml && indexHtml.includes(targetContent)) {
                                indexHtml = indexHtml.replace(targetContent, replacementContent);
                            }
                        }
                        
                        if (name === 'multi_replace_file_content' || name === 'default_api:multi_replace_file_content') {
                            let chunks = safeParse(args.ReplacementChunks || args.replacement_chunks || '[]');
                            if (targetFile.endsWith('app_final.js') && appFinal) {
                                for (let chunk of chunks) {
                                    let tc = safeParse(chunk.TargetContent || chunk.targetContent || '');
                                    let rc = safeParse(chunk.ReplacementContent || chunk.replacementContent || '');
                                    if (appFinal.includes(tc)) appFinal = appFinal.replace(tc, rc);
                                }
                            }
                            if (targetFile.endsWith('index.html') && indexHtml) {
                                for (let chunk of chunks) {
                                    let tc = safeParse(chunk.TargetContent || chunk.targetContent || '');
                                    let rc = safeParse(chunk.ReplacementContent || chunk.replacementContent || '');
                                    if (indexHtml.includes(tc)) indexHtml = indexHtml.replace(tc, rc);
                                }
                            }
                        }
                    } catch(e) {}
                }
            }
        } catch (e) {}
    }

    console.log("Restored app_final.js length:", appFinal.length);
    console.log("Restored index.html length:", indexHtml.length);

    if (appFinal.length > 0) {
        fs.writeFileSync('d:/برنامج مخازن/app_final_9am.js', appFinal, 'utf8');
    }
    if (indexHtml.length > 0) {
        fs.writeFileSync('d:/برنامج مخازن/index_9am.html', indexHtml, 'utf8');
    }
}
extract();
