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

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

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

    let appFinal = '';
    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;

                        if (name === 'write_to_file' || name === 'default_api:write_to_file') {
                            if (args.TargetFile && args.TargetFile.endsWith('app_final.js')) {
                                appFinal = args.CodeContent;
                            }
                            if (args.TargetFile && args.TargetFile.endsWith('index.html')) {
                                indexHtml = args.CodeContent;
                            }
                        }
                        
                        if (name === 'replace_file_content' || name === 'default_api:replace_file_content') {
                            if (args.TargetFile && args.TargetFile.endsWith('app_final.js') && appFinal) {
                                if (appFinal.includes(args.TargetContent)) {
                                    appFinal = appFinal.replace(args.TargetContent, args.ReplacementContent);
                                }
                            }
                            if (args.TargetFile && args.TargetFile.endsWith('index.html') && indexHtml) {
                                if (indexHtml.includes(args.TargetContent)) {
                                    indexHtml = indexHtml.replace(args.TargetContent, args.ReplacementContent);
                                }
                            }
                        }
                        
                        if (name === 'multi_replace_file_content' || name === 'default_api:multi_replace_file_content') {
                            if (args.TargetFile && args.TargetFile.endsWith('app_final.js') && appFinal) {
                                for (let chunk of args.ReplacementChunks) {
                                    if (appFinal.includes(chunk.TargetContent)) {
                                        appFinal = appFinal.replace(chunk.TargetContent, chunk.ReplacementContent);
                                    }
                                }
                            }
                            if (args.TargetFile && args.TargetFile.endsWith('index.html') && indexHtml) {
                                for (let chunk of args.ReplacementChunks) {
                                    if (indexHtml.includes(chunk.TargetContent)) {
                                        indexHtml = indexHtml.replace(chunk.TargetContent, chunk.ReplacementContent);
                                    }
                                }
                            }
                        }
                    } 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();
